6f06050f27a311c7b2aabf299e5dd86795fdf27b — Martin Angers 7 months ago 0a78636
add RouterMiddleware option
2 files changed, 12 insertions(+), 1 deletions(-)

M builder/builder.go
M kick.go
M builder/builder.go => builder/builder.go +5 -1
@@ 156,7 156,11 @@ func Handler(s *kick.Server) (http.Handler, error) {
 	// routes, respectively).
 	configureGetAndOptions(router, s.Routes)
 
-	h := combineMiddleware(router, mw)
+	routerHandler := http.Handler(router)
+	if s.Root != nil && s.Root.RouterMiddleware != nil {
+		routerHandler = s.Root.RouterMiddleware(router)
+	}
+	h := combineMiddleware(routerHandler, mw)
 	return h, nil
 }
 

M kick.go => kick.go +7 -0
@@ 395,6 395,13 @@ type Root struct {
 	// for the requested method and path.
 	NotFoundHandler http.Handler
 
+	// RouterMiddleware is a middleware function that is applied to
+	// the router itself, meaning that it's handler will run for all
+	// routes, including the NotFound and MethodNotAllowed handlers,
+	// but ensuring it runs after the root middleware so that e.g.
+	// method overrides, request IDs, etc. are applied first.
+	RouterMiddleware func(http.Handler) http.Handler
+
 	TrustProxyHeaders   bool
 	AllowMethodOverride bool