diff --git a/proxy_route.go b/proxy_route.go index fc39226..35d930f 100644 --- a/proxy_route.go +++ b/proxy_route.go @@ -50,6 +50,9 @@ type ProxyRoute struct { MiddlewareParams interface{} `json:"middleware_params" yaml:"middleware_params"` // Set to true to see errors on web page when there is a panic error on gobis ShowError bool `json:"show_error" yaml:"show_error"` + // Set to true to use full path + // e.g.: path=/metrics/** and request=/metrics/foo this will be redirected to /metrics/foo on upstream instead of /foo + UseFullPath bool `json:"use_full_path" yaml:"use_full_path"` // Chain others routes in a routes Routes []ProxyRoute `json:"routes" yaml:"routes"` // Set an handler to use to forward request to this handler when using gobis programmatically @@ -139,22 +142,28 @@ func (r ProxyRoute) RequestPath(req *http.Request) string { } func (r ProxyRoute) UpstreamUrl(req *http.Request) *url.URL { + origPath := "" + if r.UseFullPath { + origPath = r.PathAsStartPath() + "/" + } if r.ForwardHandler != nil { - req.URL.Path = "" + req.URL.Path = origPath return req.URL } var upstreamUrl *url.URL if r.ForwardedHeader == "" { upstreamUrl, _ = url.Parse(r.Url) + upstreamUrl.Path = origPath + upstreamUrl.Path return upstreamUrl } upstream := req.Header.Get(r.ForwardedHeader) if upstream == "" { upstreamUrl, _ = url.Parse(r.Url) + upstreamUrl.Path = origPath + upstreamUrl.Path return upstreamUrl } upstreamUrl, _ = url.Parse(upstream) - upstreamUrl.Path = "" + upstreamUrl.Path = origPath return upstreamUrl } diff --git a/router_factory_test.go b/router_factory_test.go index 5e16e7b..1946814 100644 --- a/router_factory_test.go +++ b/router_factory_test.go @@ -50,6 +50,16 @@ var _ = Describe("RouterFactory", func() { Expect(request.Header.Get("Authorization")).ShouldNot(BeEmpty()) }) }) + Context("when route have option UseFullPath", func() { + It("should set request url with start path from path option", func() { + ForwardRequest(ProxyRoute{ + Path: NewPathMatcher("/root"), + Url: "http://my.proxified.api", + UseFullPath: true, + }, request, "") + Expect(request.URL.String()).Should(Equal("http://my.proxified.api/root")) + }) + }) Context("when route have option ForwardedHeader", func() { It("should set request url to upstream url", func() { req, _ := http.NewRequest("GET", "http://localhost/path", nil)