Add Rule AddPrefix
This commit is contained in:
parent
318ff52ff3
commit
d9fc66fdbc
3 changed files with 43 additions and 4 deletions
22
middlewares/addPrefix.go
Normal file
22
middlewares/addPrefix.go
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package middlewares
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddPrefix is a middleware used to add prefix to an URL request
|
||||||
|
type AddPrefix struct {
|
||||||
|
Handler http.Handler
|
||||||
|
Prefix string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *AddPrefix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r.URL.Path = s.Prefix + r.URL.Path
|
||||||
|
r.RequestURI = r.URL.RequestURI()
|
||||||
|
s.Handler.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetHandler sets handler
|
||||||
|
func (s *AddPrefix) SetHandler(Handler http.Handler) {
|
||||||
|
s.Handler = Handler
|
||||||
|
}
|
8
rules.go
8
rules.go
|
@ -74,6 +74,13 @@ func (r *Rules) pathStrip(paths ...string) *mux.Route {
|
||||||
return r.route.route
|
return r.route.route
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Rules) addPrefix(paths ...string) *mux.Route {
|
||||||
|
for _, path := range paths {
|
||||||
|
r.route.addPrefix = path
|
||||||
|
}
|
||||||
|
return r.route.route
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Rules) pathPrefixStrip(paths ...string) *mux.Route {
|
func (r *Rules) pathPrefixStrip(paths ...string) *mux.Route {
|
||||||
sort.Sort(bySize(paths))
|
sort.Sort(bySize(paths))
|
||||||
r.route.stripPrefixes = paths
|
r.route.stripPrefixes = paths
|
||||||
|
@ -107,6 +114,7 @@ func (r *Rules) parseRules(expression string, onRule func(functionName string, f
|
||||||
"Method": r.methods,
|
"Method": r.methods,
|
||||||
"Headers": r.headers,
|
"Headers": r.headers,
|
||||||
"HeadersRegexp": r.headersRegexp,
|
"HeadersRegexp": r.headersRegexp,
|
||||||
|
"AddPrefix": r.addPrefix,
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(expression) == 0 {
|
if len(expression) == 0 {
|
||||||
|
|
17
server.go
17
server.go
|
@ -64,6 +64,7 @@ type serverEntryPoint struct {
|
||||||
type serverRoute struct {
|
type serverRoute struct {
|
||||||
route *mux.Route
|
route *mux.Route
|
||||||
stripPrefixes []string
|
stripPrefixes []string
|
||||||
|
addPrefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer returns an initialized Server.
|
// NewServer returns an initialized Server.
|
||||||
|
@ -718,15 +719,23 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *Server) wireFrontendBackend(serverRoute *serverRoute, handler http.Handler) {
|
func (server *Server) wireFrontendBackend(serverRoute *serverRoute, handler http.Handler) {
|
||||||
|
// add prefix
|
||||||
|
if len(serverRoute.addPrefix) > 0 {
|
||||||
|
handler = &middlewares.AddPrefix{
|
||||||
|
Prefix: serverRoute.addPrefix,
|
||||||
|
Handler: handler,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// strip prefix
|
// strip prefix
|
||||||
if len(serverRoute.stripPrefixes) > 0 {
|
if len(serverRoute.stripPrefixes) > 0 {
|
||||||
serverRoute.route.Handler(&middlewares.StripPrefix{
|
handler = &middlewares.StripPrefix{
|
||||||
Prefixes: serverRoute.stripPrefixes,
|
Prefixes: serverRoute.stripPrefixes,
|
||||||
Handler: handler,
|
Handler: handler,
|
||||||
})
|
}
|
||||||
} else {
|
|
||||||
serverRoute.route.Handler(handler)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
serverRoute.route.Handler(handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *Server) loadEntryPointConfig(entryPointName string, entryPoint *EntryPoint) (http.Handler, error) {
|
func (server *Server) loadEntryPointConfig(entryPointName string, entryPoint *EntryPoint) (http.Handler, error) {
|
||||||
|
|
Loading…
Reference in a new issue