Middlewares
This commit is contained in:
@@ -10,12 +10,13 @@ import (
|
||||
"time"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/utils"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
var MySigningKey = []byte("COUCOU")
|
||||
|
||||
type LoginInformation struct {
|
||||
type LoginArgs struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
@@ -26,13 +27,13 @@ type Claims struct {
|
||||
}
|
||||
|
||||
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Method is not allowed",
|
||||
}.Respond(w, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
// if r.Method != http.MethodPost {
|
||||
// core.JSONError{
|
||||
// Status: core.Error,
|
||||
// Message: "Method is not allowed",
|
||||
// }.Respond(w, http.StatusMethodNotAllowed)
|
||||
// return
|
||||
// }
|
||||
|
||||
if r.Body == nil {
|
||||
core.JSONError{
|
||||
@@ -50,7 +51,7 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
var login LoginInformation
|
||||
var login LoginArgs
|
||||
err = json.Unmarshal(body, &login)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
@@ -151,3 +152,25 @@ func AuthJWT(next http.Handler) http.Handler {
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// @param methods string -> HttpMethods separated by commas.
|
||||
func Methods(methods string) core.Middleware {
|
||||
_methods := strings.Split(strings.ToUpper(methods), ",")
|
||||
middleware := func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if len(methods) > 0 {
|
||||
if !utils.Contains(_methods, r.Method) {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Method is not allowed.",
|
||||
}.Respond(w, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
return middleware
|
||||
}
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -40,6 +40,8 @@ func HandleGetUser(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
user.Password = ""
|
||||
|
||||
// TODO : Remove password
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
|
||||
52
backend/api/get_users.go
Normal file
52
backend/api/get_users.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/utils"
|
||||
)
|
||||
|
||||
func HandleGetUsers(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Method is not allowed.",
|
||||
}.Respond(w, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var users []core.User
|
||||
count, err := core.DB.NewSelect().
|
||||
Model(&users).
|
||||
ScanAndCount(context.Background())
|
||||
|
||||
users = utils.Map(users, func(user core.User) core.User {
|
||||
user.Password = ""
|
||||
return user
|
||||
})
|
||||
|
||||
if count == 0 {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Not users.",
|
||||
}.Respond(w, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO : Remove password
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "Users found.",
|
||||
Data: users,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func HandleCreateUser(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
log.Println(user)
|
||||
|
||||
res, err := core.DB.NewInsert().Model(user).Exec(context.Background())
|
||||
res, err := core.DB.NewInsert().Model(&user).Exec(context.Background())
|
||||
if res == nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
|
||||
97
backend/api/update_user.go
Normal file
97
backend/api/update_user.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"fr.latosa-escrima/api/core"
|
||||
)
|
||||
|
||||
type UpdateUserArgs struct {
|
||||
FirstName *string `json:"firstname,omitempty"`
|
||||
LastName *string `json:"lastname,omitempty"`
|
||||
Email *string `json:"email,omitempty"`
|
||||
Password *string `json:"password,omitempty"`
|
||||
Phone *string `json:"phone,omitempty"`
|
||||
Role *core.Role `json:"role,omitempty"`
|
||||
}
|
||||
|
||||
func HandleUpdateUser(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPatch {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Method is not allowed.",
|
||||
}.Respond(w, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var updateArgs UpdateUserArgs
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal(body, &updateArgs)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var user core.User
|
||||
updateQuery := core.DB.NewUpdate().Model(&user)
|
||||
|
||||
val := reflect.ValueOf(updateArgs)
|
||||
typ := reflect.TypeOf(updateArgs)
|
||||
|
||||
for i := 0; i < val.NumField(); i++ {
|
||||
field := val.Field(i)
|
||||
|
||||
tag := typ.Field(i).Tag.Get("bun")
|
||||
if tag == "" {
|
||||
tag = typ.Field(i).Tag.Get("json")
|
||||
}
|
||||
|
||||
// Only add fields that are non-nil and non-zero
|
||||
if field.IsValid() && !field.IsNil() && !field.IsZero() {
|
||||
updateQuery.Set(fmt.Sprintf("%s = ?", strings.Split(tag, ",")[0]), field.Interface())
|
||||
}
|
||||
}
|
||||
|
||||
// Always update the `updated_at` field
|
||||
updateQuery.Set("updated_at = ?", time.Now())
|
||||
|
||||
uuid := r.PathValue("user_uuid")
|
||||
_, err = updateQuery.
|
||||
Where("user_id = ?", uuid).
|
||||
Returning("*").
|
||||
Exec(context.Background())
|
||||
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: err.Error(),
|
||||
}.Respond(w, http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
user.Password = ""
|
||||
|
||||
core.JSONSuccess{
|
||||
Status: core.Success,
|
||||
Message: "User updated.",
|
||||
Data: user,
|
||||
}.Respond(w, http.StatusOK)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user