Skip to content

Commit

Permalink
Merge pull request #9 from JSainsburyPLC/feature/proxy-response-headers
Browse files Browse the repository at this point in the history
proxy response headers
  • Loading branch information
webbgeorge authored Mar 17, 2020
2 parents 9f6d40d + adb406f commit 92fd892
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
```
Expand Down
15 changes: 8 additions & 7 deletions domain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions examples/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
}],
"proxy_pass_headers": {
"Referer": "https://www.test1.example.co.uk/"
},
"proxy_response_headers": {
"Cache-Control": "no-cache"
}
},
{
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
22 changes: 20 additions & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 92fd892

Please sign in to comment.