Add redirect param to user config

This commit is contained in:
Paul-Christian Volkmer 2023-09-03 09:41:00 +02:00
parent ec33b402b9
commit 80cbf9210d
4 changed files with 31 additions and 0 deletions

View File

@ -20,4 +20,5 @@ type UserConfig struct {
Alias string `toml:"alias"`
ColorScheme string `toml:"color-scheme"`
Pattern string `toml:"pattern"`
Redirect string `toml:"redirect"`
}

View File

@ -33,6 +33,12 @@ func requestHandler(w http.ResponseWriter, r *http.Request) {
for _, userConfig := range config.Users {
if icons.HashBytes(id) == icons.HashBytes(userConfig.ID) {
if userConfig.Redirect != "" {
w.Header().Add("Location", userConfig.Redirect)
w.WriteHeader(http.StatusFound)
return
}
id = userConfig.Alias
if len(userConfig.ColorScheme) > 0 {
colorScheme = userConfig.ColorScheme

View File

@ -141,3 +141,23 @@ func TestCorrectResponseForUserConfig(t *testing.T) {
t.Errorf("returned image does not match expected image for mapped alias '42'")
}
}
func TestCorrectRedirect(t *testing.T) {
configure("./testdata/testconfig.toml")
req, err := http.NewRequest("GET", "/avatar/example2", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
testRouter().ServeHTTP(rr, req)
if code := rr.Code; code != http.StatusFound {
t.Errorf("response code match: got %d want %d", code, http.StatusFound)
}
if location := rr.Header().Get("Location"); location != "https://avatars.example.com/u/42" {
t.Errorf("location header does not match: got %v want https://avatars.example.com/u/42", location)
}
}

View File

@ -6,3 +6,7 @@ id = "example"
alias = "42"
color-scheme = "gh"
pattern = "github"
[[users]]
id = "example2"
redirect = "https://avatars.example.com/u/42"