1
0
Fork 0

Correct Entrypoint Redirect with Stripped or Added Path

This commit is contained in:
Daniel Tomcej 2018-07-31 03:28:03 -06:00 committed by Traefiker Bot
parent eea60b6baa
commit 91cafd1752
6 changed files with 222 additions and 4 deletions

View file

@ -1,6 +1,7 @@
package middlewares
import (
"context"
"net/http"
)
@ -10,12 +11,21 @@ type AddPrefix struct {
Prefix string
}
type key string
const (
// AddPrefixKey is the key within the request context used to
// store the added prefix
AddPrefixKey key = "AddPrefix"
)
func (s *AddPrefix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.URL.Path = s.Prefix + r.URL.Path
if r.URL.RawPath != "" {
r.URL.RawPath = s.Prefix + r.URL.RawPath
}
r.RequestURI = r.URL.RequestURI()
r = r.WithContext(context.WithValue(r.Context(), AddPrefixKey, s.Prefix))
s.Handler.ServeHTTP(w, r)
}