initial commit
This commit is contained in:
29
pkg/sys/data.go
Normal file
29
pkg/sys/data.go
Normal file
@ -0,0 +1,29 @@
|
||||
package sys
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func ProccessRequest(w http.ResponseWriter, body io.ReadCloser) []byte {
|
||||
data, err := ioutil.ReadAll(body)
|
||||
if err != nil {
|
||||
ErrorJsonResponse(w, "could_not_proccess_request_data", err)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
|
||||
func RandStringRunes(n int) string {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
b := make([]rune, n)
|
||||
for i := range b {
|
||||
b[i] = letterRunes[rand.Intn(len(letterRunes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
55
pkg/sys/database.go
Normal file
55
pkg/sys/database.go
Normal file
@ -0,0 +1,55 @@
|
||||
package sys
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
entsql "entgo.io/ent/dialect/sql"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
"github.com/FrankenBotDev/FrankenAPI/ent"
|
||||
"github.com/FrankenBotDev/FrankenAPI/ent/migrate"
|
||||
)
|
||||
|
||||
type ContextKey string
|
||||
|
||||
const (
|
||||
EntClientKey = ContextKey("ent_client")
|
||||
)
|
||||
|
||||
const (
|
||||
ContextClientKey = ContextKey("ent_context")
|
||||
)
|
||||
|
||||
// InitDB handles postgres client creation
|
||||
func InitDB() (*ent.Client, context.Context) {
|
||||
client := open(os.Getenv("DATABASE")) // TODO move this to config
|
||||
ctx := context.Background()
|
||||
if err := client.Schema.Create(ctx); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Run migration.
|
||||
err := client.Schema.Create(
|
||||
ctx,
|
||||
migrate.WithDropIndex(true),
|
||||
migrate.WithDropColumn(true),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("failed creating schema resources: %v", err)
|
||||
}
|
||||
return client, ctx
|
||||
}
|
||||
|
||||
// open establishes a postgres connection and returns a client
|
||||
func open(databaseUrl string) *ent.Client {
|
||||
db, err := sql.Open("pgx", databaseUrl)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
drv := entsql.OpenDB(dialect.Postgres, db)
|
||||
return ent.NewClient(ent.Driver(drv))
|
||||
}
|
40
pkg/sys/permission.go
Normal file
40
pkg/sys/permission.go
Normal file
@ -0,0 +1,40 @@
|
||||
package sys
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var sysKey = "devtoken"
|
||||
|
||||
// [DEPRECATED] (This has to be redone the token system wont work as intended) might be reused on a differnt point (api access maybe)
|
||||
// ValidatePermToken validates a given token against the settings object of a given server
|
||||
/* func ValidatePermToken(permtoken string, serverid string) (error, bool) {
|
||||
token, err := sys.Client.Settings.Query().Where(settings.Serverid(serverid)).Select(settings.FieldPermtoken).All(sys.Ctx)
|
||||
|
||||
if err != nil {
|
||||
return errors.New("could_not_fetch_token_from_database"), false
|
||||
}
|
||||
|
||||
if token[0].Permtoken == permtoken {
|
||||
return nil, true
|
||||
}
|
||||
|
||||
return nil, false
|
||||
} */
|
||||
|
||||
// [deprecated] will be replaced with jwt entierly
|
||||
// ValidateAuthToken validates a given token against the settings object of a given server
|
||||
// Used to prevent third parties of using unallowed routes
|
||||
func ValidateAuthToken(toValidate string) (error, bool) {
|
||||
if reflect.TypeOf(toValidate).String() != "string" {
|
||||
return errors.New("token given is not of type string"), false
|
||||
}
|
||||
|
||||
// Validate token agains system known token
|
||||
if toValidate == sysKey {
|
||||
return nil, true
|
||||
} else {
|
||||
return nil, false
|
||||
}
|
||||
}
|
36
pkg/sys/requests.go
Normal file
36
pkg/sys/requests.go
Normal file
@ -0,0 +1,36 @@
|
||||
package sys
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SuccessJsonResponse(w http.ResponseWriter, data interface{}) {
|
||||
raw, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
// @TODO generic error
|
||||
http.Error(w, `{"type":"error", "code":"request_data_could_prepared_for_reply"}`, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(raw)
|
||||
}
|
||||
|
||||
func ErrorJsonResponse(w http.ResponseWriter, code string, err error) {
|
||||
raw, err := json.Marshal(struct {
|
||||
Type string
|
||||
Code string
|
||||
Msg string
|
||||
}{
|
||||
Type: "error",
|
||||
Code: code,
|
||||
Msg: err.Error(),
|
||||
})
|
||||
if err != nil {
|
||||
// @TODO generic error
|
||||
http.Error(w, `{"type":"error", "code":"request_data_could_prepared_for_reply"}`, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(raw)
|
||||
}
|
Reference in New Issue
Block a user