Middlewares
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type JSONStatus string
|
||||
@@ -55,33 +55,23 @@ 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 Middleware func(http.Handler) http.Handler
|
||||
|
||||
type Handler struct {
|
||||
Handler HandlerFunc
|
||||
Middleware func(http.Handler) http.Handler
|
||||
Handler http.HandlerFunc
|
||||
Middlewares []Middleware
|
||||
}
|
||||
|
||||
func HandleRoutes(mux *http.ServeMux, routes map[string]Handler) {
|
||||
for pattern, handler := range routes {
|
||||
if handler.Middleware == nil {
|
||||
if handler.Middlewares == nil {
|
||||
mux.HandleFunc(pattern, handler.Handler)
|
||||
} else {
|
||||
HandleMiddlewareRoute(
|
||||
pattern,
|
||||
handler.Handler,
|
||||
handler.Middleware,
|
||||
mux,
|
||||
)
|
||||
h := http.HandlerFunc(handler.Handler)
|
||||
for _, middleware := range handler.Middlewares {
|
||||
h = http.HandlerFunc(middleware(h).ServeHTTP)
|
||||
}
|
||||
mux.Handle(pattern, h)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ type User struct {
|
||||
FirstName string `bun:"firstname,notnull" json:"firstname"`
|
||||
LastName string `bun:"lastname,notnull" json:"lastname"`
|
||||
Email string `bun:"email,unique,notnull" json:"email"`
|
||||
Password string `bun:"password,notnull" json:"password"`
|
||||
Password string `bun:"password,notnull" json:"password,omitempty"`
|
||||
Phone string `bun:"phone,notnull" json:"phone"`
|
||||
Role Role `bun:"role,notnull,default:'user'" json:"role"`
|
||||
CreatedAt time.Time `bun:"created_at,default:current_timestamp" json:"createdAt"`
|
||||
|
||||
Reference in New Issue
Block a user