Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 92fd892

Browse files
authored
Merge pull request #9 from JSainsburyPLC/feature/proxy-response-headers
proxy response headers
2 parents 9f6d40d + adb406f commit 92fd892

File tree

7 files changed

+44
-11
lines changed

7 files changed

+44
-11
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ See `examples/config.json`
5050
}],
5151
"proxy_pass_headers": { // additional proxy headers. Optional
5252
"Referer": "https://www.test.example.com"
53+
},
54+
"proxy_response_headers": { // set response headers. Optional
55+
"Cache-Control": "no-cache"
5356
}
5457
}
5558
```

domain/config.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ type Config struct {
1717
}
1818

1919
type Route struct {
20-
Type string `json:"type"`
21-
PathPattern *PathPattern `json:"path_pattern"`
22-
Backend *Backend `json:"backend"`
23-
Mock *Mock `json:"mock"`
24-
Rewrite []Rewrite `json:"rewrite"`
25-
Redirect *Redirect `json:"redirect"`
26-
ProxyPassHeaders map[string]string `json:"proxy_pass_headers"`
20+
Type string `json:"type"`
21+
PathPattern *PathPattern `json:"path_pattern"`
22+
Backend *Backend `json:"backend"`
23+
Mock *Mock `json:"mock"`
24+
Rewrite []Rewrite `json:"rewrite"`
25+
Redirect *Redirect `json:"redirect"`
26+
ProxyPassHeaders map[string]string `json:"proxy_pass_headers"`
27+
ProxyResponseHeaders map[string]string `json:"proxy_response_headers"`
2728
}
2829

2930
type Rewrite struct {

examples/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
}],
3737
"proxy_pass_headers": {
3838
"Referer": "https://www.test1.example.co.uk/"
39+
},
40+
"proxy_response_headers": {
41+
"Cache-Control": "no-cache"
3942
}
4043
},
4144
{

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/JSainsburyPLC/ui-dev-proxy
33
go 1.13
44

55
require (
6-
github.com/steinfletcher/apitest v1.3.13
7-
github.com/stretchr/testify v1.4.0
6+
github.com/steinfletcher/apitest v1.4.4
7+
github.com/stretchr/testify v1.5.1
88
github.com/urfave/cli v1.22.1
99
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I
1111
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
1212
github.com/steinfletcher/apitest v1.3.13 h1:E0BAXde9dke8jEjK1hqTGvmI20vyyfC+xSdE9nmTc84=
1313
github.com/steinfletcher/apitest v1.3.13/go.mod h1:pCHKMM2TcH1pezw/xbmilaCdK9/dGsoCZBafwaqJ2sY=
14+
github.com/steinfletcher/apitest v1.4.4 h1:ZT/Wa3J615x7mBuarTf92o43AseYfHEFsLhnl1dBkl4=
15+
github.com/steinfletcher/apitest v1.4.4/go.mod h1:yaYc9GDlj4fa0qUUDywqAmrELlNbDrBwd36Zgo5hMZo=
1416
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1517
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
1618
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
19+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
20+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
1721
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
1822
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
1923
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

proxy/proxy.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ func NewProxy(
3232
logger *log.Logger,
3333
) *Proxy {
3434
reverseProxy := &httputil.ReverseProxy{
35-
Director: director(defaultBackend, logger),
36-
ErrorHandler: errorHandler(logger),
35+
Director: director(defaultBackend, logger),
36+
ModifyResponse: modifyResponse(),
37+
ErrorHandler: errorHandler(logger),
3738
}
3839
return &Proxy{
3940
server: &http.Server{
@@ -92,6 +93,22 @@ func director(defaultBackend *url.URL, logger *log.Logger) func(req *http.Reques
9293
}
9394
}
9495

96+
func modifyResponse() func(*http.Response) error {
97+
return func(res *http.Response) error {
98+
route, ok := res.Request.Context().Value(routeCtxKey).(*domain.Route)
99+
if !ok {
100+
// if route not set, then default backend was used and no route match config available
101+
return nil
102+
}
103+
104+
for k, v := range route.ProxyResponseHeaders {
105+
res.Header.Set(k, v)
106+
}
107+
108+
return nil
109+
}
110+
}
111+
95112
func errorHandler(logger *log.Logger) func(http.ResponseWriter, *http.Request, error) {
96113
return func(w http.ResponseWriter, r *http.Request, err error) {
97114
logger.Printf("%+v\n", err)
@@ -135,6 +152,7 @@ func handler(
135152
logger.Printf(err.Error())
136153
w.WriteHeader(http.StatusBadGateway)
137154
_, _ = w.Write([]byte("Bad gateway"))
155+
return
138156
}
139157

140158
http.Redirect(w, r, u.String(), redirectStatusCode(matchedRoute.Redirect.Type))

proxy/proxy_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func TestProxy_ProxyBackend_UserProxy_Success(t *testing.T) {
5858
Get("/test-ui/users/info").
5959
Expect(t).
6060
Status(http.StatusOK).
61+
Header("Cache-Control", "no-cache").
6162
Body(`{"user_id": "123"}`).
6263
End()
6364
}
@@ -271,6 +272,9 @@ func config() domain.Config {
271272
ProxyPassHeaders: map[string]string{
272273
"Referer": "https://www.test.example.com",
273274
},
275+
ProxyResponseHeaders: map[string]string{
276+
"Cache-Control": "no-cache",
277+
},
274278
},
275279
{
276280
Type: "proxy",

0 commit comments

Comments
 (0)