Signed-off-by: gom-by <gom-by@users.noreply.github.com>
This commit is contained in:
gom-by
2025-01-15 15:25:43 +01:00
4 changed files with 77 additions and 15 deletions

View File

@@ -75,28 +75,26 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
fmt.Println(signed) 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 { func AuthJWT(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if the Authorization header is provided // Check if the Authorization header is provided
fmt.Println("Coucou")
authHeader := r.Header.Get("Authorization") authHeader := r.Header.Get("Authorization")
if authHeader == "" { if authHeader == "" {
http.Error(w, "Missing Authorization header", http.StatusUnauthorized) JSONError{
Status: Error,
Message: "Missing Authorization header",
}.Respond(w, http.StatusUnauthorized)
return return
} }
// Bearer token is expected, so split the header into "Bearer <token>" // Bearer token is expected, so split the header into "Bearer <token>"
tokenString := strings.TrimPrefix(authHeader, "Bearer ") tokenString := strings.TrimPrefix(authHeader, "Bearer ")
if tokenString == authHeader { 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 return
} }
@@ -110,7 +108,10 @@ func AuthJWT(next http.Handler) http.Handler {
}) })
if err != nil || !token.Valid { if err != nil || !token.Valid {
http.Error(w, "Invalid token", http.StatusUnauthorized) JSONError{
Status: Error,
Message: "Invalid Token",
}.Respond(w, http.StatusUnauthorized)
return return
} }

View File

@@ -8,7 +8,7 @@ import (
core "fr.latosa-escrima/api/core" 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") w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodPost { if r.Method != http.MethodPost {

View File

@@ -50,7 +50,7 @@ func main() {
HandleRoutes(mux, map[string]Handler{ HandleRoutes(mux, map[string]Handler{
"/": { handler, nil}, "/": { handler, nil},
"/users/login": { api.HandleLogin, nil}, "/users/login": { api.HandleLogin, nil},
// "/users/new": { api.HandleCreateUser, api.AuthJWT}, "/users/new": { api.HandleCreateUser, api.AuthJWT},
// "/blogs": { api.HandleGetBlogs, nil}, // "/blogs": { api.HandleGetBlogs, nil},
"/blogs/new": { api.HandleCreateBlog, nil}, "/blogs/new": { api.HandleCreateBlog, nil},
"/blogs/{uuid}": { api.HandleGetBlog, nil}, "/blogs/{uuid}": { api.HandleGetBlog, nil},

View File

@@ -3,8 +3,69 @@ package main
import ( import (
"net/http" "net/http"
api "fr.latosa-escrima/api" 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 HandlerFunc func(w http.ResponseWriter, r *http.Request)
type Handler struct { type Handler struct {
@@ -17,7 +78,7 @@ func HandleRoutes(mux *http.ServeMux, routes map[string]Handler) {
if handler.Middleware == nil { if handler.Middleware == nil {
mux.HandleFunc(pattern, handler.Handler) mux.HandleFunc(pattern, handler.Handler)
} else { } else {
api.HandleMiddlewareRoute( HandleMiddlewareRoute(
pattern, pattern,
handler.Handler, handler.Handler,
handler.Middleware, handler.Middleware,