diff --git a/config.go b/config.go new file mode 100644 index 0000000..b6ead09 --- /dev/null +++ b/config.go @@ -0,0 +1,23 @@ +package main + +// Config holds the configuration for Idicon service. +type Config struct { + Defaults Defaults `toml:"defaults"` + Users []UserConfig `toml:"users"` +} + +// Defaults holds default configuration values to be used as defaults for all users. +type Defaults struct { + ColorScheme string `toml:"color-scheme"` + Pattern string `toml:"pattern"` +} + +// UserConfig holds user specific configuration. +// ID is the id od the user in plain text, +// Alias is the alias to be used to generate the id icon. +type UserConfig struct { + ID string `toml:"id"` + Alias string `toml:"alias"` + ColorScheme string `toml:"color-scheme"` + Pattern string `toml:"pattern"` +} diff --git a/idicon.go b/idicon.go index 5ded080..e24d955 100644 --- a/idicon.go +++ b/idicon.go @@ -124,7 +124,7 @@ func drawImage(data []bool, blocks int, size int, c color.Color) *image.NRGBA { return img } -func RequestHandler(w http.ResponseWriter, r *http.Request) { +func requestHandler(w http.ResponseWriter, r *http.Request) { id := mux.Vars(r)["id"] size, err := strconv.Atoi(r.URL.Query().Get("s")) @@ -143,7 +143,7 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) { } for _, userConfig := range config.Users { - if hashBytes(id) == hashBytes(userConfig.Id) { + if hashBytes(id) == hashBytes(userConfig.ID) { id = userConfig.Alias if len(userConfig.ColorScheme) > 0 { colorScheme = userConfig.ColorScheme @@ -170,23 +170,6 @@ func RequestHandler(w http.ResponseWriter, r *http.Request) { } -type Config struct { - Defaults Defaults `toml:"defaults"` - Users []UserConfig `toml:"users"` -} - -type Defaults struct { - ColorScheme string `toml:"color-scheme"` - Pattern string `toml:"pattern"` -} - -type UserConfig struct { - Id string `toml:"id"` - Alias string `toml:"alias"` - ColorScheme string `toml:"color-scheme"` - Pattern string `toml:"pattern"` -} - var ( config Config ) @@ -219,7 +202,7 @@ func main() { configure(*configFile) router := mux.NewRouter() - router.HandleFunc("/avatar/{id}", RequestHandler) + router.HandleFunc("/avatar/{id}", requestHandler) log.Println("Starting ...") log.Fatal(http.ListenAndServe(":8000", router)) } diff --git a/idicon_test.go b/idicon_test.go index c025fa5..1988ef6 100644 --- a/idicon_test.go +++ b/idicon_test.go @@ -27,7 +27,7 @@ var gh2 []byte func testRouter() *mux.Router { router := mux.NewRouter() - router.HandleFunc("/avatar/{id}", RequestHandler) + router.HandleFunc("/avatar/{id}", requestHandler) return router } @@ -116,7 +116,7 @@ func TestUsesConfig(t *testing.T) { configure("./testdata/testconfig.toml") if config.Defaults.ColorScheme != "gh" || - config.Users[0].Id != "example" || + config.Users[0].ID != "example" || config.Users[0].Alias != "42" || config.Users[0].ColorScheme != "gh" || config.Users[0].Pattern != "github" { @@ -131,7 +131,7 @@ func TestUsesConfigWithEnvVar(t *testing.T) { configure("./testdata/testconfig.toml") if config.Defaults.ColorScheme != "v1" || - config.Users[0].Id != "example" || + config.Users[0].ID != "example" || config.Users[0].Alias != "42" || config.Users[0].ColorScheme != "gh" || config.Users[0].Pattern != "github" {