chore: started working #36 and closes #12

This commit is contained in:
2023-10-06 12:13:19 +01:00
parent 16a6ae844b
commit 8d5f2a829a
10 changed files with 299 additions and 290 deletions

View File

@@ -344,6 +344,7 @@ type Context struct {
User *dbtypes.User
Mode AnswerType
Logger *log.Logger
Db *sql.DB
}
func (c Context) Error400(err error, message string, w http.ResponseWriter, path string, base string, data AnyMap) *Error {
@@ -409,7 +410,7 @@ func (c *Context) requireAuth(w http.ResponseWriter, r *http.Request) bool {
var LogoffError = errors.New("Invalid token!")
func (x Handle) createContext(mode AnswerType, r *http.Request) (*Context, error) {
func (x Handle) createContext(handler *Handle, mode AnswerType, r *http.Request) (*Context, error) {
var token *string
@@ -438,7 +439,7 @@ func (x Handle) createContext(mode AnswerType, r *http.Request) (*Context, error
Prefix: r.URL.Path,
})
return &Context{token, user, mode, logger}, nil
return &Context{token, user, mode, logger, handler.Db}, nil
}
// TODO check if I can use http.Redirect
@@ -516,6 +517,7 @@ func ErrorCode(err error, code int, data AnyMap) *Error {
}
func Error500(err error) *Error {
log.Warn("This function is deprecated please use the one provided by context")
return ErrorCode(err, http.StatusInternalServerError, nil)
}
@@ -563,7 +565,7 @@ func NewHandler(db *sql.DB) *Handle {
//TODO JSON
//Login state
context, err := x.createContext(ans, r)
context, err := x.createContext(x, ans, r)
if err != nil {
Logoff(ans, w, r)
return