Skip to content

Commit dcbe333

Browse files
authored
feat: use x-forwarded-prefix to generate links (#87)
Resolves #86.
1 parent 7daee19 commit dcbe333

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ ingress:
6565
- envelope-zero.example.com
6666
```
6767
68+
If you do not use the root path `/`, but a prefix, make sure your reverse proxy writes the used prefix in the `x-forwarded-prefix` header. That header is used by the backend to generate the correct URLs for resources.
69+
6870
## Supported Versions
6971

7072
This project is under heavy development. Therefore, only the latest release is supported.

internal/controllers/helper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func requestURL(c *gin.Context) string {
3737
scheme = "https"
3838
}
3939

40-
return scheme + "://" + c.Request.Host + c.Request.URL.Path
40+
forwardedPrefix := c.Request.Header.Get("x-forwarded-prefix")
41+
return scheme + "://" + c.Request.Host + forwardedPrefix + c.Request.URL.Path
4142
}
4243

4344
// FetchErrorHandler handles errors for fetching data from the database.

internal/controllers/helper_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,15 @@ func TestRequestURLHTTPS(t *testing.T) {
1919

2020
assert.Equal(t, "https:///v1/budgets", apiResponse.Links["budgets"])
2121
}
22+
23+
func TestRequestForwardedPrefix(t *testing.T) {
24+
recorder := test.Request(t, "GET", "/v1", "", map[string]string{"x-forwarded-prefix": "/api"})
25+
26+
var apiResponse test.APIResponse
27+
err := json.NewDecoder(recorder.Body).Decode(&apiResponse)
28+
if err != nil {
29+
assert.Fail(t, "Unable to parse response from server %q into APIResponse, '%v'", recorder.Body, err)
30+
}
31+
32+
assert.Equal(t, "http:///api/v1/budgets", apiResponse.Links["budgets"])
33+
}

0 commit comments

Comments
 (0)