From 0759b72aecaaf40b02af1ebf032e5a23d7a4bedf Mon Sep 17 00:00:00 2001 From: Plankiton Date: Sat, 12 Dec 2020 23:56:01 -0300 Subject: [PATCH] bugfix: set automatic redirection on routes without "/" on end of path Always I to create a route on old version with "/user" as example, when I request "/user/" it returns a 404 error, to fix this I made the mux to create de route with "/" on end, and vice versa --- mux.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mux.go b/mux.go index 782a34b..9132ed9 100644 --- a/mux.go +++ b/mux.go @@ -297,7 +297,12 @@ func (r *Router) Handle(path string, handler http.Handler) *Route { // See Route.Path() and Route.HandlerFunc(). func (r *Router) HandleFunc(path string, f func(http.ResponseWriter, *http.Request)) *Route { - return r.NewRoute().Path(path).HandlerFunc(f) + if path[len(path)-1] != '/' { + r.NewRoute().Path(path+"/").HandlerFunc(f) + } else { + r.NewRoute().Path(path[:len(path)-1]).HandlerFunc(f) + } + return r.NewRoute().Path(path).HandlerFunc(f) } // Headers registers a new route with a matcher for request header values.