Skip to content

Commit

Permalink
Merge pull request #931 from Juliens/addprefix
Browse files Browse the repository at this point in the history
Add Rule AddPrefix
  • Loading branch information
vdemeester authored Dec 21, 2016
2 parents 6b20d2a + 6ca142b commit e1ed8b7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Frontends can be defined using the following rules:
- `PathStrip`: Same as `Path` but strip the given prefix from the request URL's Path.
- `PathPrefix`: PathPrefix adds a matcher for the URL path prefixes. This matches if the given template is a prefix of the full URL path.
- `PathPrefixStrip`: Same as `PathPrefix` but strip the given prefix from the request URL's Path.
- `AddPrefix` : Add prefix from the request URL's Path.

You can use multiple rules by separating them by `;`

Expand Down
22 changes: 22 additions & 0 deletions middlewares/addPrefix.go
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ func (r *Rules) pathStrip(paths ...string) *mux.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 {
sort.Sort(bySize(paths))
r.route.stripPrefixes = paths
Expand Down Expand Up @@ -107,6 +114,7 @@ func (r *Rules) parseRules(expression string, onRule func(functionName string, f
"Method": r.methods,
"Headers": r.headers,
"HeadersRegexp": r.headersRegexp,
"AddPrefix": r.addPrefix,
}

if len(expression) == 0 {
Expand Down
17 changes: 13 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type serverEntryPoint struct {
type serverRoute struct {
route *mux.Route
stripPrefixes []string
addPrefix string
}

// NewServer returns an initialized Server.
Expand Down Expand Up @@ -716,15 +717,23 @@ func (server *Server) loadConfig(configurations configs, globalConfiguration Glo
}

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
if len(serverRoute.stripPrefixes) > 0 {
serverRoute.route.Handler(&middlewares.StripPrefix{
handler = &middlewares.StripPrefix{
Prefixes: serverRoute.stripPrefixes,
Handler: handler,
})
} else {
serverRoute.route.Handler(handler)
}
}

serverRoute.route.Handler(handler)
}

func (server *Server) loadEntryPointConfig(entryPointName string, entryPoint *EntryPoint) (http.Handler, error) {
Expand Down

0 comments on commit e1ed8b7

Please sign in to comment.