Skip to content

Commit

Permalink
middleware.PathRewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Jul 14, 2021
1 parent 69b8558 commit 2e1364a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions middleware/page_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 16 additions & 0 deletions middleware/path_rewrite.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}

0 comments on commit 2e1364a

Please sign in to comment.