From 6f311de45a7a72ed0e4a2b16869194782e701bdf Mon Sep 17 00:00:00 2001 From: Romain Tadiello Date: Fri, 27 Jan 2023 14:00:15 +0100 Subject: [PATCH] Make CleanPath middleware work with other routers --- middleware/clean_path.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/middleware/clean_path.go b/middleware/clean_path.go index adeba429..d77476ab 100644 --- a/middleware/clean_path.go +++ b/middleware/clean_path.go @@ -12,17 +12,19 @@ import ( func CleanPath(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rctx := chi.RouteContext(r.Context()) - - routePath := rctx.RoutePath + + var routePath string + if rctx != nil { + routePath = rctx.RoutePath + } if routePath == "" { if r.URL.RawPath != "" { routePath = r.URL.RawPath } else { routePath = r.URL.Path } - rctx.RoutePath = path.Clean(routePath) } - + rctx.RoutePath = path.Clean(routePath) next.ServeHTTP(w, r) }) }