Skip to content

Instantly share code, notes, and snippets.

@piotrkubisa
Created September 29, 2017 11:12
Show Gist options
  • Save piotrkubisa/3c1f0e4756edfb78deb33bb6e13af9b2 to your computer and use it in GitHub Desktop.
Save piotrkubisa/3c1f0e4756edfb78deb33bb6e13af9b2 to your computer and use it in GitHub Desktop.
StripLeadingSlashes for chi
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