Created
September 29, 2017 11:12
-
-
Save piotrkubisa/3c1f0e4756edfb78deb33bb6e13af9b2 to your computer and use it in GitHub Desktop.
StripLeadingSlashes for chi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package server | |
func StripLeadingSlashes(next http.Handler) http.Handler { | |
fn := func(w http.ResponseWriter, r *http.Request) { | |
var path string | |
rctx := chi.RouteContext(r.Context()) | |
if rctx.RoutePath != "" { | |
path = rctx.RoutePath | |
} else { | |
path = r.URL.Path | |
} | |
if len(path) > 2 && path[0:2] == "//" { | |
rctx.RoutePath = path[1:len(path)] | |
} | |
next.ServeHTTP(w, r) | |
} | |
return http.HandlerFunc(fn) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment