middleware.PathRewrite
This commit is contained in:
parent
69b85589c6
commit
2e1364ace2
2 changed files with 18 additions and 2 deletions
|
|
@ -8,13 +8,13 @@ import (
|
|||
// PageRoute is a simple middleware which allows you to route a static GET request
|
||||
// at the middleware stack level.
|
||||
func PageRoute(path string, handler http.Handler) func(http.Handler) http.Handler {
|
||||
return func(h http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == "GET" && strings.EqualFold(r.URL.Path, path) {
|
||||
handler.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
h.ServeHTTP(w, r)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
16
middleware/path_rewrite.go
Normal file
16
middleware/path_rewrite.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// PathRewrite is a simple middleware which allows you to rewrite the request URL path.
|
||||
func PathRewrite(old, new string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.URL.Path = strings.Replace(r.URL.Path, old, new, 1)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue