-
-
Notifications
You must be signed in to change notification settings - Fork 553
/
chi.go
59 lines (56 loc) Β· 1.64 KB
/
chi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package middleware
import (
"fmt"
"net/http"
"github.com/go-chi/chi/v5"
)
// SmartRedirectSlashes is a middleware that matches the request path with
// patterns added to the router and redirects it.
//
// If a pattern is added to the router with a trailing slash, any matches on
// that pattern without a trailing slash will be redirected to the version with
// the slash. If a pattern does not have a trailing slash, matches on that
// pattern with a trailing slash will be redirected to the version without.
//
// This middleware depends on chi, so it needs to be mounted on chi's router.
// It make the router behavior similar to httptreemux.
func SmartRedirectSlashes(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
rctx := chi.RouteContext(r.Context())
if rctx != nil {
var path string
if rctx.RoutePath != "" {
path = rctx.RoutePath
} else {
path = r.URL.Path
}
var method string
if rctx.RouteMethod != "" {
method = rctx.RouteMethod
} else {
method = r.Method
}
if len(path) > 1 {
if rctx.Routes != nil {
if !rctx.Routes.Match(chi.NewRouteContext(), method, path) {
if path[len(path)-1] == '/' {
path = path[:len(path)-1]
} else {
path += "/"
}
if rctx.Routes.Match(chi.NewRouteContext(), method, path) {
if r.URL.RawQuery != "" {
path = fmt.Sprintf("%s?%s", path, r.URL.RawQuery)
}
redirectURL := fmt.Sprintf("//%s%s", r.Host, path)
http.Redirect(w, r, redirectURL, http.StatusMovedPermanently)
return
}
}
}
}
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}