diff --git a/internal/middleware/middleware.go b/internal/middleware/middleware.go index 274ac50..4745a86 100644 --- a/internal/middleware/middleware.go +++ b/internal/middleware/middleware.go @@ -8,6 +8,7 @@ import ( "net/http" "net/netip" "regexp" + "strings" "time" basicauth "github.com/99designs/basicauth-go" @@ -250,6 +251,17 @@ func routePattern(r *http.Request) string { } pattern = rctx.Routes.Find(chi.NewRouteContext(), r.Method, path) + + // For the path a subrouter is mounted at, such as /v1 or /v1/, Find + // answers that path without asking the subrouter, which chi hands + // the request as /. + for _, route := range rctx.Routes.Routes() { + if route.SubRoutes != nil && + route.Pattern == strings.TrimSuffix(pattern, "/")+"/*" && + !route.SubRoutes.Match(chi.NewRouteContext(), r.Method, "/") { + pattern = "" + } + } } if pattern == "" {