From d9fc66fdbc42d387edb8f4bbd56a4d8d4ecf6f93 Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Fri, 2 Dec 2016 13:40:18 +0100 Subject: [PATCH 1/2] Add Rule AddPrefix --- middlewares/addPrefix.go | 22 ++++++++++++++++++++++ rules.go | 8 ++++++++ server.go | 17 +++++++++++++---- 3 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 middlewares/addPrefix.go diff --git a/middlewares/addPrefix.go b/middlewares/addPrefix.go new file mode 100644 index 000000000..467ca7cf3 --- /dev/null +++ b/middlewares/addPrefix.go @@ -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 +} diff --git a/rules.go b/rules.go index 3bb6edca7..811ff3292 100644 --- a/rules.go +++ b/rules.go @@ -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 @@ -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 { diff --git a/server.go b/server.go index f771f786f..571863df3 100644 --- a/server.go +++ b/server.go @@ -64,6 +64,7 @@ type serverEntryPoint struct { type serverRoute struct { route *mux.Route stripPrefixes []string + addPrefix string } // 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) { + // 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) { From a4034ce1e292e492cc3068349a9ee94555591529 Mon Sep 17 00:00:00 2001 From: Julien Salleyron Date: Mon, 19 Dec 2016 17:17:24 +0100 Subject: [PATCH 2/2] Add some docs --- docs/basics.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/basics.md b/docs/basics.md index 1103aee81..eb2a231fc 100644 --- a/docs/basics.md +++ b/docs/basics.md @@ -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 `;`