diff --git a/backend/api/auth.go b/backend/api/auth.go index 7529bb2..aaf8363 100644 --- a/backend/api/auth.go +++ b/backend/api/auth.go @@ -75,28 +75,26 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) { fmt.Println(signed) } -func HandleMiddlewareRoute(pattern string, - handler func(w http.ResponseWriter, r *http.Request), - middleware func(http.Handler) http.Handler, - mux *http.ServeMux, -) { - mux.HandleFunc(pattern, handler) - http.Handle(pattern, middleware(mux)) -} - func AuthJWT(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Check if the Authorization header is provided + fmt.Println("Coucou") authHeader := r.Header.Get("Authorization") if authHeader == "" { - http.Error(w, "Missing Authorization header", http.StatusUnauthorized) + JSONError{ + Status: Error, + Message: "Missing Authorization header", + }.Respond(w, http.StatusUnauthorized) return } // Bearer token is expected, so split the header into "Bearer " tokenString := strings.TrimPrefix(authHeader, "Bearer ") if tokenString == authHeader { - http.Error(w, "Invalid Authorization header format", http.StatusUnauthorized) + JSONError{ + Status: Error, + Message: "Invalid Authorization header format", + }.Respond(w, http.StatusUnauthorized) return } @@ -110,7 +108,10 @@ func AuthJWT(next http.Handler) http.Handler { }) if err != nil || !token.Valid { - http.Error(w, "Invalid token", http.StatusUnauthorized) + JSONError{ + Status: Error, + Message: "Invalid Token", + }.Respond(w, http.StatusUnauthorized) return } diff --git a/backend/api/new_user.go b/backend/api/new_user.go index 615bcec..63ce902 100644 --- a/backend/api/new_user.go +++ b/backend/api/new_user.go @@ -8,7 +8,7 @@ import ( core "fr.latosa-escrima/api/core" ) -func handlerCreateUser(w http.ResponseWriter, r *http.Request) { +func HandleCreateUser(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if r.Method != http.MethodPost { diff --git a/backend/main.go b/backend/main.go index 954cf36..8c9414b 100644 --- a/backend/main.go +++ b/backend/main.go @@ -50,7 +50,7 @@ func main() { HandleRoutes(mux, map[string]Handler{ "/": { handler, nil}, "/users/login": { api.HandleLogin, nil}, - // "/users/new": { api.HandleCreateUser, api.AuthJWT}, + "/users/new": { api.HandleCreateUser, api.AuthJWT}, // "/blogs": { api.HandleGetBlogs, nil}, "/blogs/new": { api.HandleCreateBlog, nil}, "/blogs/{uuid}": { api.HandleGetBlog, nil}, diff --git a/backend/router.go b/backend/router.go index 0bb167a..ae1e599 100644 --- a/backend/router.go +++ b/backend/router.go @@ -3,8 +3,69 @@ package main import ( "net/http" api "fr.latosa-escrima/api" + "encoding/json" + core "fr.latosa-escrima/api/core" ) +type JSONStatus string + +const ( + Error JSONStatus = "Error" + Success JSONStatus = "Success" +) + +type JSONResponse interface { + ToJSON() ([]byte, error) + Respond(w http.ResponseWriter, code int) +} + +type JSONError struct { + Status JSONStatus `json:"status"` + Message string `json:"message"` +} + +type JSONSuccess struct { + Status JSONStatus `json:"status"` + Message string `json:"message"` + Data any `json:"data,omitempty"` +} + +func (r *JSONError) ToJSON() ([]byte, error) { + return json.Marshal(r) +} + +func (r *JSONSuccess) ToJSON() ([]byte, error) { + return json.Marshal(r) +} + +func defaultResponse(r JSONResponse, w http.ResponseWriter, code int) { + jsonData, err := r.ToJSON() + if err != nil { + http.Error(w, err.Error(), http.StatusNotAcceptable) + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(code) + w.Write(jsonData) +} + +func (r JSONError) Respond(w http.ResponseWriter, code int) { + defaultResponse(&r, w, code) +} + +func (r JSONSuccess) Respond(w http.ResponseWriter, code int) { + defaultResponse(&r, w, code) +} + +func HandleMiddlewareRoute(pattern string, + handler func(w http.ResponseWriter, r *http.Request), + middleware func(http.Handler) http.Handler, + mux *http.ServeMux, +) { + // mux.HandleFunc(pattern, handler) + mux.Handle(pattern, middleware(http.HandlerFunc(handler))) +} + type HandlerFunc func(w http.ResponseWriter, r *http.Request) type Handler struct { @@ -17,7 +78,7 @@ func HandleRoutes(mux *http.ServeMux, routes map[string]Handler) { if handler.Middleware == nil { mux.HandleFunc(pattern, handler.Handler) } else { - api.HandleMiddlewareRoute( + HandleMiddlewareRoute( pattern, handler.Handler, handler.Middleware,