WIP Svelte
This commit is contained in:
parent
38180f8afd
commit
ded0e8e1c8
48 changed files with 3976 additions and 46 deletions
37
handler.go
37
handler.go
|
@ -8,6 +8,7 @@ import (
|
|||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
@ -18,7 +19,6 @@ func Router() *httprouter.Router {
|
|||
return router
|
||||
}
|
||||
|
||||
|
||||
type HTTPResponse interface {
|
||||
WriteResponse(http.ResponseWriter)
|
||||
}
|
||||
|
@ -62,9 +62,19 @@ func (r APIErrorResponse) WriteResponse(w http.ResponseWriter) {
|
|||
http.Error(w, fmt.Sprintf("{\"errmsg\":%q}", r.err.Error()), r.status)
|
||||
}
|
||||
|
||||
|
||||
type DispatchFunction func(httprouter.Params, []byte) HTTPResponse
|
||||
|
||||
func eraseCookie(w http.ResponseWriter) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: "auth",
|
||||
Value: "",
|
||||
Path: baseURL + "/",
|
||||
Expires: time.Unix(0, 0),
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteStrictMode,
|
||||
})
|
||||
}
|
||||
|
||||
func rawHandler(f func(http.ResponseWriter, *http.Request, httprouter.Params, []byte), access ...func(*User, *http.Request) *APIErrorResponse) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
|
||||
|
@ -76,16 +86,19 @@ func rawHandler(f func(http.ResponseWriter, *http.Request, httprouter.Params, []
|
|||
var user *User = nil
|
||||
if cookie, err := r.Cookie("auth"); err == nil {
|
||||
if sessionid, err := base64.StdEncoding.DecodeString(cookie.Value); err != nil {
|
||||
eraseCookie(w)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err.Error()), http.StatusNotAcceptable)
|
||||
return
|
||||
} else if session, err := getSession(sessionid); err != nil {
|
||||
eraseCookie(w)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err.Error()), http.StatusUnauthorized)
|
||||
return
|
||||
} else if session.IdUser == nil {
|
||||
user = nil
|
||||
} else if std, err := getUser(int(*session.IdUser)); err != nil {
|
||||
eraseCookie(w)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
http.Error(w, fmt.Sprintf(`{"errmsg": %q}`, err.Error()), http.StatusUnauthorized)
|
||||
return
|
||||
|
@ -134,22 +147,22 @@ func formatResponseHandler(f func(*http.Request, httprouter.Params, []byte) HTTP
|
|||
}
|
||||
|
||||
func apiRawHandler(f func(http.ResponseWriter, httprouter.Params, []byte) HTTPResponse, access ...func(*User, *http.Request) *APIErrorResponse) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||
return rawHandler(func (w http.ResponseWriter, r *http.Request, ps httprouter.Params, b []byte) {
|
||||
formatResponseHandler(func (_ *http.Request, ps httprouter.Params, b []byte) HTTPResponse {
|
||||
return rawHandler(func(w http.ResponseWriter, r *http.Request, ps httprouter.Params, b []byte) {
|
||||
formatResponseHandler(func(_ *http.Request, ps httprouter.Params, b []byte) HTTPResponse {
|
||||
return f(w, ps, b)
|
||||
})(w, r, ps, b)
|
||||
}, access...)
|
||||
}
|
||||
|
||||
func apiHandler(f DispatchFunction, access ...func(*User, *http.Request) *APIErrorResponse) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||
return rawHandler(formatResponseHandler(func (_ *http.Request, ps httprouter.Params, b []byte) HTTPResponse { return f(ps, b) }), access...)
|
||||
return rawHandler(formatResponseHandler(func(_ *http.Request, ps httprouter.Params, b []byte) HTTPResponse { return f(ps, b) }), access...)
|
||||
}
|
||||
|
||||
func formatApiResponse(i interface{}, err error) HTTPResponse {
|
||||
if err != nil {
|
||||
return APIErrorResponse{
|
||||
status: http.StatusBadRequest,
|
||||
err: err,
|
||||
err: err,
|
||||
}
|
||||
} else {
|
||||
return APIResponse{i}
|
||||
|
@ -157,25 +170,25 @@ func formatApiResponse(i interface{}, err error) HTTPResponse {
|
|||
}
|
||||
|
||||
func apiAuthHandler(f func(*User, httprouter.Params, []byte) HTTPResponse, access ...func(*User, *http.Request) *APIErrorResponse) func(http.ResponseWriter, *http.Request, httprouter.Params) {
|
||||
return rawHandler(formatResponseHandler(func (r *http.Request, ps httprouter.Params, b []byte) HTTPResponse {
|
||||
return rawHandler(formatResponseHandler(func(r *http.Request, ps httprouter.Params, b []byte) HTTPResponse {
|
||||
if cookie, err := r.Cookie("auth"); err != nil {
|
||||
return f(nil, ps, b)
|
||||
} else if sessionid, err := base64.StdEncoding.DecodeString(cookie.Value); err != nil {
|
||||
return APIErrorResponse{
|
||||
status: http.StatusBadRequest,
|
||||
err: err,
|
||||
err: err,
|
||||
}
|
||||
} else if session, err := getSession(sessionid); err != nil {
|
||||
return APIErrorResponse{
|
||||
status: http.StatusBadRequest,
|
||||
err: err,
|
||||
err: err,
|
||||
}
|
||||
} else if session.IdUser == nil {
|
||||
return f(nil, ps, b)
|
||||
} else if std, err := getUser(int(*session.IdUser)); err != nil {
|
||||
return APIErrorResponse{
|
||||
status: http.StatusInternalServerError,
|
||||
err: err,
|
||||
err: err,
|
||||
}
|
||||
} else {
|
||||
return f(&std, ps, b)
|
||||
|
@ -189,7 +202,7 @@ func loggedUser(u *User, r *http.Request) *APIErrorResponse {
|
|||
} else {
|
||||
ret := &APIErrorResponse{
|
||||
status: http.StatusForbidden,
|
||||
err: errors.New("Permission Denied"),
|
||||
err: errors.New("Permission Denied"),
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
@ -201,7 +214,7 @@ func adminRestricted(u *User, r *http.Request) *APIErrorResponse {
|
|||
} else {
|
||||
ret := &APIErrorResponse{
|
||||
status: http.StatusForbidden,
|
||||
err: errors.New("Permission Denied"),
|
||||
err: errors.New("Permission Denied"),
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
|
Reference in a new issue