chore: added config file and fixed updates not showing while training

This commit is contained in:
2024-04-08 15:47:31 +01:00
parent de0b430467
commit 2faf90f462
9 changed files with 91 additions and 50 deletions

View File

@@ -42,6 +42,7 @@ type Handle struct {
gets []HandleFunc
posts []HandleFunc
deletes []HandleFunc
Config Config
}
func decodeBody(r *http.Request) (string, *Error) {
@@ -89,7 +90,7 @@ func (x *Handle) handleGets(context *Context) {
return
}
}
context.ShowMessage = false
context.ShowMessage = false
handleError(&Error{404, "Endpoint not found"}, context)
}
@@ -100,7 +101,7 @@ func (x *Handle) handlePosts(context *Context) {
return
}
}
context.ShowMessage = false
context.ShowMessage = false
handleError(&Error{404, "Endpoint not found"}, context)
}
@@ -111,7 +112,7 @@ func (x *Handle) handleDeletes(context *Context) {
return
}
}
context.ShowMessage = false
context.ShowMessage = false
handleError(&Error{404, "Endpoint not found"}, context)
}
@@ -138,6 +139,7 @@ type Context struct {
R *http.Request
Tx *sql.Tx
ShowMessage bool
Handle *Handle
}
func (c Context) Prepare(str string) (*sql.Stmt, error) {
@@ -315,11 +317,12 @@ func (x Handle) createContext(handler *Handle, r *http.Request, w http.ResponseW
// TODO check that the token is still valid
if token == nil {
return &Context{
Logger: logger,
Db: handler.Db,
Writer: w,
R: r,
ShowMessage: true,
Logger: logger,
Db: handler.Db,
Writer: w,
R: r,
ShowMessage: true,
Handle: &x,
}, nil
}
@@ -328,7 +331,7 @@ func (x Handle) createContext(handler *Handle, r *http.Request, w http.ResponseW
return nil, errors.Join(err, LogoffError)
}
return &Context{token, user, logger, handler.Db, w, r, nil, true}, nil
return &Context{token, user, logger, handler.Db, w, r, nil, true, &x}, nil
}
func contextlessLogoff(w http.ResponseWriter) {
@@ -468,12 +471,12 @@ func (x Handle) ReadTypesFilesApi(pathTest string, baseFilePath string, fileType
})
}
func NewHandler(db *sql.DB) *Handle {
func NewHandler(db *sql.DB, config Config) *Handle {
var gets []HandleFunc
var posts []HandleFunc
var deletes []HandleFunc
x := &Handle{db, gets, posts, deletes}
x := &Handle{db, gets, posts, deletes, config}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@@ -482,11 +485,11 @@ func NewHandler(db *sql.DB) *Handle {
w.Header().Add("Access-Control-Allow-Methods", "*")
// Decide answertype
if !(r.Header.Get("content-type") == "application/json" || r.Header.Get("response-type") == "application/json") {
/* if !(r.Header.Get("content-type") == "application/json" || r.Header.Get("response-type") == "application/json") {
w.WriteHeader(500)
w.Write([]byte("Please set content-type to application/json or set response-type to application/json\n"))
return
}
}*/
if !strings.HasPrefix(r.URL.Path, "/api") {
w.WriteHeader(404)
@@ -513,13 +516,13 @@ func NewHandler(db *sql.DB) *Handle {
x.handleDeletes(context)
} else if r.Method == "OPTIONS" {
// do nothing
} else {
panic("TODO handle method: " + r.Method)
}
if context.ShowMessage {
context.Logger.Info("Processed", "method", r.Method, "url", r.URL.Path)
}
} else {
panic("TODO handle method: " + r.Method)
}
if context.ShowMessage {
context.Logger.Info("Processed", "method", r.Method, "url", r.URL.Path)
}
})
return x
@@ -528,10 +531,5 @@ func NewHandler(db *sql.DB) *Handle {
func (x Handle) Startup() {
log.Info("Starting up!\n")
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", x.Config.Port), nil))
}