Add new debug option for http

This commit is contained in:
Tom Wiesing 2023-11-22 17:28:46 +01:00
parent 0ba34fe80f
commit 0290a42d07
No known key found for this signature in database
39 changed files with 293 additions and 189 deletions

View file

@ -4,7 +4,7 @@ import (
"context"
"net/http"
"github.com/tkw1536/pkglib/mux"
"github.com/tkw1536/pkglib/httpx/mux"
)
// Routeable is a component that is servable
@ -51,10 +51,29 @@ type Routes struct {
Decorator func(http.Handler) http.Handler
}
type routeContextTyp int
const routeContextKey routeContextTyp = 0
// RouteContext represents the context passed to a given route
type RouteContext struct {
DefaultDomain bool
}
// WithRouteContext adds the given RouteContext to the context
func WithRouteContext(parent context.Context, value RouteContext) context.Context {
return context.WithValue(parent, routeContextKey, value)
}
// RouteContextOf returns the route context of the given context
func RouteContextOf(context context.Context) RouteContext {
ctx, ok := context.Value(routeContextKey).(RouteContext)
if !ok {
return RouteContext{}
}
return ctx
}
// Predicate returns the predicate corresponding to the given route
func (routes Routes) Predicate(context func(*http.Request) RouteContext) mux.Predicate {
if routes.MatchAllDomains || routes.Internal {