feat: add tasks closes #74
This commit is contained in:
@@ -7,10 +7,18 @@ import (
|
||||
"github.com/charmbracelet/log"
|
||||
)
|
||||
|
||||
type WorkerConfig struct {
|
||||
NumberOfWorkers int `toml:"number_of_workers"`
|
||||
Pulling string `toml:"pulling_time"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Hostname string
|
||||
Port int
|
||||
NumberOfWorkers int `toml:"number_of_workers"`
|
||||
NumberOfWorkers int `toml:"number_of_workers"`
|
||||
SupressCuda int `toml:"supress_cuda"`
|
||||
|
||||
GpuWorker WorkerConfig `toml:"Worker"`
|
||||
}
|
||||
|
||||
func LoadConfig() Config {
|
||||
@@ -25,10 +33,21 @@ func LoadConfig() Config {
|
||||
Hostname: "localhost",
|
||||
Port: 8000,
|
||||
NumberOfWorkers: 10,
|
||||
GpuWorker: WorkerConfig{
|
||||
NumberOfWorkers: 1,
|
||||
Pulling: "500ms",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var conf Config
|
||||
_, err = toml.Decode(string(dat), &conf)
|
||||
|
||||
if conf.SupressCuda == 1 {
|
||||
log.Warn("Supressing Cuda Messages!")
|
||||
os.Setenv("TF_CPP_MIN_VLOG_LEVEL", "3")
|
||||
os.Setenv("TF_CPP_MIN_LOG_LEVEL", "3")
|
||||
}
|
||||
|
||||
return conf
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ func handleError(err *Error, c *Context) {
|
||||
e = c.SendJSON(500)
|
||||
}
|
||||
if e != nil {
|
||||
c.Logger.Error("Something went very wront while trying to send and error message")
|
||||
c.Logger.Error("Something went very wrong while trying to send and error message")
|
||||
c.Writer.Write([]byte("505"))
|
||||
}
|
||||
}
|
||||
@@ -81,7 +81,6 @@ func (x *Handle) Post(path string, fn func(c *Context) *Error) {
|
||||
x.posts = append(x.posts, HandleFunc{path, fn})
|
||||
}
|
||||
|
||||
|
||||
func (x *Handle) PostAuth(path string, authLevel int, fn func(c *Context) *Error) {
|
||||
inner_fn := func(c *Context) *Error {
|
||||
if !c.CheckAuthLevel(authLevel) {
|
||||
@@ -97,6 +96,13 @@ func (x *Handle) Delete(path string, fn func(c *Context) *Error) {
|
||||
}
|
||||
|
||||
func (x *Handle) handleGets(context *Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
context.Logger.Error("Something went very wrong", "Error", r)
|
||||
handleError(&Error{500, "500"}, context)
|
||||
}
|
||||
}()
|
||||
|
||||
for _, s := range x.gets {
|
||||
if s.path == context.R.URL.Path {
|
||||
handleError(s.fn(context), context)
|
||||
@@ -108,6 +114,13 @@ func (x *Handle) handleGets(context *Context) {
|
||||
}
|
||||
|
||||
func (x *Handle) handlePosts(context *Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
context.Logger.Error("Something went very wrong", "Error", r)
|
||||
handleError(&Error{500, "500"}, context)
|
||||
}
|
||||
}()
|
||||
|
||||
for _, s := range x.posts {
|
||||
if s.path == context.R.URL.Path {
|
||||
handleError(s.fn(context), context)
|
||||
@@ -119,6 +132,13 @@ func (x *Handle) handlePosts(context *Context) {
|
||||
}
|
||||
|
||||
func (x *Handle) handleDeletes(context *Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
context.Logger.Error("Something went very wrong", "Error", r)
|
||||
handleError(&Error{500, "500"}, context)
|
||||
}
|
||||
}()
|
||||
|
||||
for _, s := range x.deletes {
|
||||
if s.path == context.R.URL.Path {
|
||||
handleError(s.fn(context), context)
|
||||
@@ -155,6 +175,20 @@ type Context struct {
|
||||
Handle *Handle
|
||||
}
|
||||
|
||||
|
||||
func (c Context) GetDb() (*sql.DB) {
|
||||
return c.Db
|
||||
}
|
||||
|
||||
func (c Context) GetLogger() (*log.Logger) {
|
||||
return c.Logger
|
||||
}
|
||||
|
||||
func (c Context) Query(query string, args ...any) (*sql.Rows, error) {
|
||||
return c.Db.Query(query, args...)
|
||||
}
|
||||
|
||||
|
||||
func (c Context) Prepare(str string) (*sql.Stmt, error) {
|
||||
if c.Tx == nil {
|
||||
return c.Db.Prepare(str)
|
||||
@@ -199,19 +233,32 @@ func (c *Context) RollbackTx() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse and vailidates the json
|
||||
*/
|
||||
func (c Context) ParseJson(dat any, str string) *Error {
|
||||
decoder := json.NewDecoder(strings.NewReader(str))
|
||||
|
||||
return c.decodeAndValidade(decoder, dat)
|
||||
}
|
||||
|
||||
func (c Context) ToJSON(dat any) *Error {
|
||||
|
||||
decoder := json.NewDecoder(c.R.Body)
|
||||
|
||||
return c.decodeAndValidade(decoder, dat)
|
||||
}
|
||||
|
||||
func (c Context) decodeAndValidade(decoder *json.Decoder, dat any) *Error {
|
||||
err := decoder.Decode(dat)
|
||||
if err != nil {
|
||||
return c.Error500(err)
|
||||
c.Logger.Error("Failed to decode json", "dat", dat, "err", err)
|
||||
return c.JsonBadRequest("Bad Request! Invalid json passed!");
|
||||
}
|
||||
|
||||
err = c.Handle.validate.Struct(dat)
|
||||
if err != nil {
|
||||
c.Logger.Error("Failed invalid json passed", "dat", dat, "err", err)
|
||||
return c.JsonBadRequest("Bad Request! Invalid body passed!")
|
||||
return c.JsonBadRequest("Bad Request! Invalid json passed!");
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -246,7 +293,7 @@ func (c Context) JsonBadRequest(dat any) *Error {
|
||||
c.SetReportCaller(true)
|
||||
c.Logger.Warn("Request failed with a bad request", "dat", dat)
|
||||
c.SetReportCaller(false)
|
||||
return c.SendJSONStatus(http.StatusBadRequest, dat)
|
||||
return c.ErrorCode(nil, 404, dat)
|
||||
}
|
||||
|
||||
func (c Context) JsonErrorBadRequest(err error, dat any) *Error {
|
||||
@@ -308,6 +355,10 @@ func (c Context) Error500(err error) *Error {
|
||||
return c.ErrorCode(err, http.StatusInternalServerError, nil)
|
||||
}
|
||||
|
||||
func (c Context) E500M(msg string, err error) *Error {
|
||||
return c.ErrorCode(err, http.StatusInternalServerError, msg)
|
||||
}
|
||||
|
||||
func (c *Context) requireAuth() bool {
|
||||
if c.User == nil {
|
||||
return true
|
||||
|
||||
@@ -12,9 +12,29 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/log"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
|
||||
type BasePack interface {
|
||||
GetDb() *sql.DB
|
||||
GetLogger() *log.Logger
|
||||
}
|
||||
|
||||
type BasePackStruct struct {
|
||||
Db *sql.DB
|
||||
Logger *log.Logger
|
||||
}
|
||||
|
||||
func (b BasePackStruct) GetDb() (*sql.DB) {
|
||||
return b.Db
|
||||
}
|
||||
|
||||
func (b BasePackStruct) GetLogger() (*log.Logger) {
|
||||
return b.Logger
|
||||
}
|
||||
|
||||
func CheckEmpty(f url.Values, path string) bool {
|
||||
return !f.Has(path) || f.Get(path) == ""
|
||||
}
|
||||
@@ -184,7 +204,7 @@ func MyParseForm(r *http.Request) (vs url.Values, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
type JustId struct { Id string }
|
||||
type JustId struct{ Id string }
|
||||
|
||||
type Generic struct{ reflect.Type }
|
||||
|
||||
@@ -196,34 +216,39 @@ func generateQuery(t reflect.Type) (query string, nargs int) {
|
||||
query = ""
|
||||
|
||||
for i := 0; i < nargs; i += 1 {
|
||||
field := t.Field(i)
|
||||
name, ok := field.Tag.Lookup("db")
|
||||
if !ok {
|
||||
name = field.Name;
|
||||
}
|
||||
field := t.Field(i)
|
||||
name, ok := field.Tag.Lookup("db")
|
||||
if !ok {
|
||||
name = field.Name
|
||||
}
|
||||
|
||||
if name == "__nil__" {
|
||||
continue
|
||||
}
|
||||
query += strings.ToLower(name) + ","
|
||||
if name == "__nil__" {
|
||||
continue
|
||||
}
|
||||
query += strings.ToLower(name) + ","
|
||||
}
|
||||
|
||||
// Remove the last comma
|
||||
|
||||
// Remove the last comma
|
||||
query = query[0 : len(query)-1]
|
||||
|
||||
return
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func GetDbMultitple[T interface{}](c *Context, tablename string, args ...any) ([]*T, error) {
|
||||
t := reflect.TypeFor[T]()
|
||||
type QueryInterface interface {
|
||||
Prepare(str string) (*sql.Stmt, error)
|
||||
Query(query string, args ...any) (*sql.Rows, error)
|
||||
}
|
||||
|
||||
query, nargs := generateQuery(t)
|
||||
func GetDbMultitple[T interface{}](c QueryInterface, tablename string, args ...any) ([]*T, error) {
|
||||
t := reflect.TypeFor[T]()
|
||||
|
||||
db_query, err := c.Prepare(fmt.Sprintf("select %s from %s", query, tablename))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer db_query.Close()
|
||||
query, nargs := generateQuery(t)
|
||||
|
||||
db_query, err := c.Prepare(fmt.Sprintf("select %s from %s", query, tablename))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer db_query.Close()
|
||||
|
||||
rows, err := db_query.Query(args...)
|
||||
if err != nil {
|
||||
@@ -231,55 +256,55 @@ func GetDbMultitple[T interface{}](c *Context, tablename string, args ...any) ([
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
list := []*T{}
|
||||
list := []*T{}
|
||||
|
||||
for rows.Next() {
|
||||
item := new(T)
|
||||
if err = mapRow(item, rows, nargs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list = append(list, item)
|
||||
}
|
||||
for rows.Next() {
|
||||
item := new(T)
|
||||
if err = mapRow(item, rows, nargs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list = append(list, item)
|
||||
}
|
||||
|
||||
return list, nil
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func mapRow(store interface{}, rows *sql.Rows, nargs int) (err error) {
|
||||
err = nil
|
||||
err = nil
|
||||
|
||||
val := reflect.Indirect(reflect.ValueOf(store))
|
||||
scan_args := make([]interface{}, nargs);
|
||||
for i := 0; i < nargs; i++ {
|
||||
valueField := val.Field(i)
|
||||
scan_args[i] = valueField.Addr().Interface()
|
||||
}
|
||||
val := reflect.Indirect(reflect.ValueOf(store))
|
||||
scan_args := make([]interface{}, nargs)
|
||||
for i := 0; i < nargs; i++ {
|
||||
valueField := val.Field(i)
|
||||
scan_args[i] = valueField.Addr().Interface()
|
||||
}
|
||||
|
||||
err = rows.Scan(scan_args...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func InsertReturnId(c *Context, store interface{}, tablename string, returnName string) (id string, err error) {
|
||||
t := reflect.TypeOf(store).Elem()
|
||||
t := reflect.TypeOf(store).Elem()
|
||||
|
||||
query, nargs := generateQuery(t)
|
||||
query, nargs := generateQuery(t)
|
||||
|
||||
query2 := ""
|
||||
for i := 0; i < nargs; i += 1 {
|
||||
query2 += fmt.Sprintf("$%d,", i)
|
||||
}
|
||||
// Remove last quotation
|
||||
query2 := ""
|
||||
for i := 0; i < nargs; i += 1 {
|
||||
query2 += fmt.Sprintf("$%d,", i+1)
|
||||
}
|
||||
// Remove last quotation
|
||||
query2 = query2[0 : len(query2)-1]
|
||||
|
||||
val := reflect.ValueOf(store).Elem()
|
||||
scan_args := make([]interface{}, nargs);
|
||||
for i := 0; i < nargs; i++ {
|
||||
valueField := val.Field(i)
|
||||
scan_args[i] = valueField.Interface()
|
||||
}
|
||||
val := reflect.ValueOf(store).Elem()
|
||||
scan_args := make([]interface{}, nargs)
|
||||
for i := 0; i < nargs; i++ {
|
||||
valueField := val.Field(i)
|
||||
scan_args[i] = valueField.Interface()
|
||||
}
|
||||
|
||||
rows, err := c.Db.Query(fmt.Sprintf("insert into %s (%s) values (%s) returning %s", tablename, query, query2, returnName), scan_args...)
|
||||
if err != nil {
|
||||
@@ -296,15 +321,15 @@ func InsertReturnId(c *Context, store interface{}, tablename string, returnName
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
func GetDBOnce(c *Context, store interface{}, tablename string, args ...any) error {
|
||||
t := reflect.TypeOf(store).Elem()
|
||||
func GetDBOnce(db QueryInterface, store interface{}, tablename string, args ...any) error {
|
||||
t := reflect.TypeOf(store).Elem()
|
||||
|
||||
query, nargs := generateQuery(t)
|
||||
query, nargs := generateQuery(t)
|
||||
|
||||
rows, err := c.Db.Query(fmt.Sprintf("select %s from %s", query, tablename), args...)
|
||||
rows, err := db.Query(fmt.Sprintf("select %s from %s", query, tablename), args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -314,20 +339,24 @@ func GetDBOnce(c *Context, store interface{}, tablename string, args ...any) err
|
||||
return NotFoundError
|
||||
}
|
||||
|
||||
err = nil
|
||||
err = nil
|
||||
|
||||
val := reflect.ValueOf(store).Elem()
|
||||
scan_args := make([]interface{}, nargs);
|
||||
for i := 0; i < nargs; i++ {
|
||||
valueField := val.Field(i)
|
||||
scan_args[i] = valueField.Addr().Interface()
|
||||
}
|
||||
val := reflect.ValueOf(store).Elem()
|
||||
scan_args := make([]interface{}, nargs)
|
||||
for i := 0; i < nargs; i++ {
|
||||
valueField := val.Field(i)
|
||||
scan_args[i] = valueField.Addr().Interface()
|
||||
}
|
||||
|
||||
err = rows.Scan(scan_args...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateStatus(c *Context, table string, id string, status int) (err error) {
|
||||
_, err = c.Db.Exec(fmt.Sprintf("update %s set status = $1 where id = $2", table), status, id)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user