feat(smalltrip,problem): middleware support to set global handlers

This commit is contained in:
Guz
2025-07-30 19:14:43 -03:00
parent 47670daf55
commit 5069ac6478

View File

@@ -0,0 +1,32 @@
package problem
import (
"context"
"net/http"
"forge.capytal.company/loreddev/x/smalltrip/middleware"
)
func Middleware(h Handler) middleware.Middleware {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), contextKey, h)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
func HandlerMiddleware(fallback ...Handler) Handler {
return func(p Problem) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler := r.Context().Value(contextKey)
if h, ok := handler.(Handler); handler != nil && ok {
h(p).ServeHTTP(w, r)
} else if len(fallback) > 0 {
fallback[0](p).ServeHTTP(w, r)
}
})
}
}
var contextKey = "x-smalltrip-problems-middleware-handler"