From adb406ff10363059b8382dd1fbf169007ef610f4 Mon Sep 17 00:00:00 2001 From: George Webb Date: Mon, 16 Mar 2020 17:23:07 +0000 Subject: [PATCH] proxy response headers --- README.md | 3 +++ domain/config.go | 15 ++++++++------- examples/config.json | 3 +++ go.mod | 4 ++-- go.sum | 4 ++++ proxy/proxy.go | 22 ++++++++++++++++++++-- proxy/proxy_test.go | 4 ++++ 7 files changed, 44 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ba59afa..8a9cc3f 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,9 @@ See `examples/config.json` }], "proxy_pass_headers": { // additional proxy headers. Optional "Referer": "https://www.test.example.com" + }, + "proxy_response_headers": { // set response headers. Optional + "Cache-Control": "no-cache" } } ``` diff --git a/domain/config.go b/domain/config.go index 059749e..1540eae 100644 --- a/domain/config.go +++ b/domain/config.go @@ -17,13 +17,14 @@ type Config struct { } type Route struct { - Type string `json:"type"` - PathPattern *PathPattern `json:"path_pattern"` - Backend *Backend `json:"backend"` - Mock *Mock `json:"mock"` - Rewrite []Rewrite `json:"rewrite"` - Redirect *Redirect `json:"redirect"` - ProxyPassHeaders map[string]string `json:"proxy_pass_headers"` + Type string `json:"type"` + PathPattern *PathPattern `json:"path_pattern"` + Backend *Backend `json:"backend"` + Mock *Mock `json:"mock"` + Rewrite []Rewrite `json:"rewrite"` + Redirect *Redirect `json:"redirect"` + ProxyPassHeaders map[string]string `json:"proxy_pass_headers"` + ProxyResponseHeaders map[string]string `json:"proxy_response_headers"` } type Rewrite struct { diff --git a/examples/config.json b/examples/config.json index 7101adc..0137bfb 100644 --- a/examples/config.json +++ b/examples/config.json @@ -36,6 +36,9 @@ }], "proxy_pass_headers": { "Referer": "https://www.test1.example.co.uk/" + }, + "proxy_response_headers": { + "Cache-Control": "no-cache" } }, { diff --git a/go.mod b/go.mod index c999066..7d29a2a 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/JSainsburyPLC/ui-dev-proxy go 1.13 require ( - github.com/steinfletcher/apitest v1.3.13 - github.com/stretchr/testify v1.4.0 + github.com/steinfletcher/apitest v1.4.4 + github.com/stretchr/testify v1.5.1 github.com/urfave/cli v1.22.1 ) diff --git a/go.sum b/go.sum index 51103b4..2baeb60 100644 --- a/go.sum +++ b/go.sum @@ -11,9 +11,13 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/steinfletcher/apitest v1.3.13 h1:E0BAXde9dke8jEjK1hqTGvmI20vyyfC+xSdE9nmTc84= github.com/steinfletcher/apitest v1.3.13/go.mod h1:pCHKMM2TcH1pezw/xbmilaCdK9/dGsoCZBafwaqJ2sY= +github.com/steinfletcher/apitest v1.4.4 h1:ZT/Wa3J615x7mBuarTf92o43AseYfHEFsLhnl1dBkl4= +github.com/steinfletcher/apitest v1.4.4/go.mod h1:yaYc9GDlj4fa0qUUDywqAmrELlNbDrBwd36Zgo5hMZo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/proxy/proxy.go b/proxy/proxy.go index e3d3732..c738959 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -32,8 +32,9 @@ func NewProxy( logger *log.Logger, ) *Proxy { reverseProxy := &httputil.ReverseProxy{ - Director: director(defaultBackend, logger), - ErrorHandler: errorHandler(logger), + Director: director(defaultBackend, logger), + ModifyResponse: modifyResponse(), + ErrorHandler: errorHandler(logger), } return &Proxy{ server: &http.Server{ @@ -92,6 +93,22 @@ func director(defaultBackend *url.URL, logger *log.Logger) func(req *http.Reques } } +func modifyResponse() func(*http.Response) error { + return func(res *http.Response) error { + route, ok := res.Request.Context().Value(routeCtxKey).(*domain.Route) + if !ok { + // if route not set, then default backend was used and no route match config available + return nil + } + + for k, v := range route.ProxyResponseHeaders { + res.Header.Set(k, v) + } + + return nil + } +} + func errorHandler(logger *log.Logger) func(http.ResponseWriter, *http.Request, error) { return func(w http.ResponseWriter, r *http.Request, err error) { logger.Printf("%+v\n", err) @@ -135,6 +152,7 @@ func handler( logger.Printf(err.Error()) w.WriteHeader(http.StatusBadGateway) _, _ = w.Write([]byte("Bad gateway")) + return } http.Redirect(w, r, u.String(), redirectStatusCode(matchedRoute.Redirect.Type)) diff --git a/proxy/proxy_test.go b/proxy/proxy_test.go index 7be9b37..b3c708b 100644 --- a/proxy/proxy_test.go +++ b/proxy/proxy_test.go @@ -58,6 +58,7 @@ func TestProxy_ProxyBackend_UserProxy_Success(t *testing.T) { Get("/test-ui/users/info"). Expect(t). Status(http.StatusOK). + Header("Cache-Control", "no-cache"). Body(`{"user_id": "123"}`). End() } @@ -271,6 +272,9 @@ func config() domain.Config { ProxyPassHeaders: map[string]string{ "Referer": "https://www.test.example.com", }, + ProxyResponseHeaders: map[string]string{ + "Cache-Control": "no-cache", + }, }, { Type: "proxy",