Compare commits
3 Commits
54e37e59fc
...
738243d945
| Author | SHA1 | Date | |
|---|---|---|---|
| 738243d945 | |||
| 4fbdfb6e4a | |||
| 6bb3a78b88 |
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -290,3 +291,24 @@ func HandleServiceError(w http.ResponseWriter, err error, defaultMsg string, def
|
|||||||
SendErrorResponse(w, defaultMsg, defaultCode)
|
SendErrorResponse(w, defaultMsg, defaultCode)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetValidatedDTO[T any](r *http.Request) (*T, bool) {
|
||||||
|
dtoVal := middleware.GetValidatedDTOFromContext(r.Context())
|
||||||
|
if dtoVal == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
dto, ok := dtoVal.(*T)
|
||||||
|
return dto, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithValidation[T any](validationMiddleware func(http.Handler) http.Handler, handler http.HandlerFunc) http.HandlerFunc {
|
||||||
|
if validationMiddleware == nil {
|
||||||
|
return handler
|
||||||
|
}
|
||||||
|
var zero T
|
||||||
|
dtoType := reflect.TypeOf(zero)
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
ctx := middleware.SetDTOTypeInContext(r.Context(), dtoType)
|
||||||
|
validationMiddleware(handler).ServeHTTP(w, r.WithContext(ctx))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,4 +18,5 @@ type RouteModuleConfig struct {
|
|||||||
AuthRateLimit func(chi.Router) chi.Router
|
AuthRateLimit func(chi.Router) chi.Router
|
||||||
CSRFMiddleware func(http.Handler) http.Handler
|
CSRFMiddleware func(http.Handler) http.Handler
|
||||||
AuthMiddleware func(http.Handler) http.Handler
|
AuthMiddleware func(http.Handler) http.Handler
|
||||||
|
ValidationMiddleware func(http.Handler) http.Handler
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ func NewRouter(cfg RouterConfig) http.Handler {
|
|||||||
},
|
},
|
||||||
CSRFMiddleware: middleware.CSRFMiddleware(),
|
CSRFMiddleware: middleware.CSRFMiddleware(),
|
||||||
AuthMiddleware: middleware.NewAuth(cfg.AuthService),
|
AuthMiddleware: middleware.NewAuth(cfg.AuthService),
|
||||||
|
ValidationMiddleware: middleware.ValidationMiddleware(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.PageHandler != nil {
|
if cfg.PageHandler != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user