2016-02-26 14:29:53 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// StripPrefix is a middleware used to strip prefix from an URL request
|
|
|
|
type StripPrefix struct {
|
|
|
|
Handler http.Handler
|
|
|
|
Prefix string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StripPrefix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if p := strings.TrimPrefix(r.URL.Path, s.Prefix); len(p) < len(r.URL.Path) {
|
|
|
|
r.URL.Path = p
|
|
|
|
r.RequestURI = r.URL.RequestURI()
|
|
|
|
s.Handler.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
}
|
2016-03-27 00:05:17 +00:00
|
|
|
|
|
|
|
// SetHandler sets handler
|
|
|
|
func (s *StripPrefix) SetHandler(Handler http.Handler) {
|
|
|
|
s.Handler = Handler
|
|
|
|
}
|