chore: more work on moving to svelte front end

This commit is contained in:
2024-03-01 23:03:25 +00:00
parent ce866725ff
commit e990b832d3
22 changed files with 1799 additions and 218 deletions

View File

@@ -346,6 +346,7 @@ type Context struct {
Mode AnswerType
Logger *log.Logger
Db *sql.DB
Writer http.ResponseWriter
}
func (c Context) ToJSON(r *http.Request, dat any) *Error {
@@ -360,33 +361,40 @@ func (c Context) ToJSON(r *http.Request, dat any) *Error {
return nil
}
func (c Context) SendJSON(w http.ResponseWriter, dat any) *Error {
w.Header().Add("content-type", "application/json")
func (c Context) SendJSON(dat any) *Error {
c.Writer.Header().Add("content-type", "application/json")
text, err := json.Marshal(dat)
if err != nil {
return c.Error500(err)
}
if _, err = w.Write(text); err != nil {
if _, err = c.Writer.Write(text); err != nil {
return c.Error500(err)
}
return nil
}
func (c Context) SendJSONStatus(w http.ResponseWriter, status int, dat any) *Error {
w.Header().Add("content-type", "application/json")
w.WriteHeader(status)
func (c Context) SendJSONStatus(status int, dat any) *Error {
c.Writer.Header().Add("content-type", "application/json")
c.Writer.WriteHeader(status)
text, err := json.Marshal(dat)
if err != nil {
return c.Error500(err)
}
if _, err = w.Write(text); err != nil {
if _, err = c.Writer.Write(text); err != nil {
return c.Error500(err)
}
return nil
}
func (c Context) JsonBadRequest(w http.ResponseWriter, dat any) *Error {
return c.SendJSONStatus(w, http.StatusBadRequest, dat)
func (c Context) JsonBadRequest(dat any) *Error {
return c.SendJSONStatus(http.StatusBadRequest, dat)
}
func (c Context) JsonErrorBadRequest(err error, dat any) *Error {
c.SetReportCaller(true)
c.Logger.Error("Error while processing request", "err", err, "dat", dat)
c.SetReportCaller(false)
return c.SendJSONStatus(http.StatusBadRequest, dat)
}
func (c Context) Error400(err error, message string, w http.ResponseWriter, path string, base string, data AnyMap) *Error {
@@ -460,7 +468,7 @@ func (c *Context) requireAuth(w http.ResponseWriter, r *http.Request) bool {
var LogoffError = errors.New("Invalid token!")
func (x Handle) createContext(handler *Handle, mode AnswerType, r *http.Request) (*Context, error) {
func (x Handle) createContext(handler *Handle, mode AnswerType, r *http.Request, w http.ResponseWriter) (*Context, error) {
var token *string
@@ -486,12 +494,12 @@ func (x Handle) createContext(handler *Handle, mode AnswerType, r *http.Request)
// TODO check that the token is still valid
if token == nil {
return &Context{
Mode: mode,
Logger: logger,
Db: handler.Db,
Writer: w,
}, nil
}
@@ -500,7 +508,7 @@ func (x Handle) createContext(handler *Handle, mode AnswerType, r *http.Request)
return nil, errors.Join(err, LogoffError)
}
return &Context{token, user, mode, logger, handler.Db}, nil
return &Context{token, user, mode, logger, handler.Db, w}, nil
}
// TODO check if I can use http.Redirect
@@ -664,21 +672,28 @@ func NewHandler(db *sql.DB) *Handle {
if r.Header.Get("Request-Type") == "htmlfull" {
ans = HTMLFULL
}
if r.Header.Get("content-type") == "application/json" {
if r.Header.Get("content-type") == "application/json" || r.Header.Get("response-type") == "application/json" {
ans = JSON
}
//TODO JSON
if !strings.HasPrefix(r.URL.Path, "/api") {
return
}
r.URL.Path = strings.Replace(r.URL.Path, "/api", "", 1)
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "*")
w.Header().Add("Access-Control-Allow-Methods", "*")
//Login state
context, err := x.createContext(x, ans, r)
context, err := x.createContext(x, ans, r, w)
if err != nil {
Logoff(ans, w, r)
return
}
w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "*")
// context.Logger.Info("Parsing", "path", r.URL.Path)
if r.Method == "GET" {
x.handleGets(w, r, context)