You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.7 KiB
63 lines
1.7 KiB
4 years ago
|
package middleware
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
|
||
3 years ago
|
"github.com/go-chi/chi/v5"
|
||
4 years ago
|
)
|
||
|
|
||
|
// StripSlashes is a middleware that will match request paths with a trailing
|
||
|
// slash, strip it from the path and continue routing through the mux, if a route
|
||
|
// matches, then it will serve the handler.
|
||
|
func StripSlashes(next http.Handler) http.Handler {
|
||
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||
|
var path string
|
||
|
rctx := chi.RouteContext(r.Context())
|
||
4 years ago
|
if rctx != nil && rctx.RoutePath != "" {
|
||
4 years ago
|
path = rctx.RoutePath
|
||
|
} else {
|
||
|
path = r.URL.Path
|
||
|
}
|
||
|
if len(path) > 1 && path[len(path)-1] == '/' {
|
||
4 years ago
|
newPath := path[:len(path)-1]
|
||
|
if rctx == nil {
|
||
|
r.URL.Path = newPath
|
||
|
} else {
|
||
|
rctx.RoutePath = newPath
|
||
|
}
|
||
4 years ago
|
}
|
||
|
next.ServeHTTP(w, r)
|
||
|
}
|
||
|
return http.HandlerFunc(fn)
|
||
|
}
|
||
|
|
||
|
// RedirectSlashes is a middleware that will match request paths with a trailing
|
||
|
// slash and redirect to the same path, less the trailing slash.
|
||
|
//
|
||
|
// NOTE: RedirectSlashes middleware is *incompatible* with http.FileServer,
|
||
|
// see https://github.com/go-chi/chi/issues/343
|
||
|
func RedirectSlashes(next http.Handler) http.Handler {
|
||
|
fn := func(w http.ResponseWriter, r *http.Request) {
|
||
|
var path string
|
||
|
rctx := chi.RouteContext(r.Context())
|
||
4 years ago
|
if rctx != nil && rctx.RoutePath != "" {
|
||
4 years ago
|
path = rctx.RoutePath
|
||
|
} else {
|
||
|
path = r.URL.Path
|
||
|
}
|
||
|
if len(path) > 1 && path[len(path)-1] == '/' {
|
||
|
if r.URL.RawQuery != "" {
|
||
|
path = fmt.Sprintf("%s?%s", path[:len(path)-1], r.URL.RawQuery)
|
||
|
} else {
|
||
|
path = path[:len(path)-1]
|
||
|
}
|
||
4 years ago
|
redirectURL := fmt.Sprintf("//%s%s", r.Host, path)
|
||
|
http.Redirect(w, r, redirectURL, 301)
|
||
4 years ago
|
return
|
||
|
}
|
||
|
next.ServeHTTP(w, r)
|
||
|
}
|
||
|
return http.HandlerFunc(fn)
|
||
|
}
|