diff --git a/config.go b/config.go index b6ead09..05fa350 100644 --- a/config.go +++ b/config.go @@ -20,4 +20,5 @@ type UserConfig struct { Alias string `toml:"alias"` ColorScheme string `toml:"color-scheme"` Pattern string `toml:"pattern"` + Redirect string `toml:"redirect"` } diff --git a/idicon.go b/idicon.go index 9141062..1efda1e 100644 --- a/idicon.go +++ b/idicon.go @@ -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 diff --git a/idicon_test.go b/idicon_test.go index b508e76..e8c2d31 100644 --- a/idicon_test.go +++ b/idicon_test.go @@ -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) + } +} diff --git a/testdata/testconfig.toml b/testdata/testconfig.toml index 452de3a..4a6bae7 100644 --- a/testdata/testconfig.toml +++ b/testdata/testconfig.toml @@ -6,3 +6,7 @@ id = "example" alias = "42" color-scheme = "gh" pattern = "github" + +[[users]] +id = "example2" +redirect = "https://avatars.example.com/u/42" \ No newline at end of file