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

Commit 9f6d40d

Browse files
Add support for setting headers on the proxy (#7)
1 parent 31291f5 commit 9f6d40d

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ See `examples/config.json`
4848
"path_pattern": "/test-ui/(.*)",
4949
"to": "/$1",
5050
}],
51+
"proxy_pass_headers": { // additional proxy headers. Optional
52+
"Referer": "https://www.test.example.com"
53+
}
5154
}
5255
```
5356

domain/config.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ 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"`
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"`
2627
}
2728

2829
type Rewrite struct {

examples/config.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
"rewrite": [{
3434
"path_pattern": "/test-ui/(.*)",
3535
"to": "/$1"
36-
}]
36+
}],
37+
"proxy_pass_headers": {
38+
"Referer": "https://www.test1.example.co.uk/"
39+
}
3740
},
3841
{
3942
"type": "redirect",

proxy/proxy.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ func director(defaultBackend *url.URL, logger *log.Logger) func(req *http.Reques
8484
break
8585
}
8686
}
87+
88+
// set any proxy pass headers from config
89+
for name, value := range route.ProxyPassHeaders {
90+
req.Header.Set(name, value)
91+
}
8792
}
8893
}
8994

proxy/proxy_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ func otherProxyMock(status int, responseBody string) *apitest.Mock {
238238

239239
func userProxyMock(status int, responseBody string) *apitest.Mock {
240240
return apitest.NewMock().Get("http://localhost:3001/test-ui/users/info").
241+
Header("Referer", "https://www.test.example.com").
241242
RespondWith().
242243
Status(status).
243244
Body(responseBody).
@@ -267,6 +268,9 @@ func config() domain.Config {
267268
Type: "proxy",
268269
PathPattern: &domain.PathPattern{Regexp: regexp.MustCompile("^/test-ui/users/.*")},
269270
Backend: &domain.Backend{URL: mockProxyUrlUserUi},
271+
ProxyPassHeaders: map[string]string{
272+
"Referer": "https://www.test.example.com",
273+
},
270274
},
271275
{
272276
Type: "proxy",

0 commit comments

Comments
 (0)