mirror of
				https://github.com/pcvolkmer/idicon.git
				synced 2025-10-31 18:16:11 +00:00 
			
		
		
		
	Extract configuration types
This commit is contained in:
		
							
								
								
									
										23
									
								
								config.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								config.go
									
									
									
									
									
										Normal file
									
								
							| @@ -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"` | ||||||
|  | } | ||||||
							
								
								
									
										23
									
								
								idicon.go
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								idicon.go
									
									
									
									
									
								
							| @@ -124,7 +124,7 @@ func drawImage(data []bool, blocks int, size int, c color.Color) *image.NRGBA { | |||||||
| 	return img | 	return img | ||||||
| } | } | ||||||
|  |  | ||||||
| func RequestHandler(w http.ResponseWriter, r *http.Request) { | func requestHandler(w http.ResponseWriter, r *http.Request) { | ||||||
| 	id := mux.Vars(r)["id"] | 	id := mux.Vars(r)["id"] | ||||||
|  |  | ||||||
| 	size, err := strconv.Atoi(r.URL.Query().Get("s")) | 	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 { | 	for _, userConfig := range config.Users { | ||||||
| 		if hashBytes(id) == hashBytes(userConfig.Id) { | 		if hashBytes(id) == hashBytes(userConfig.ID) { | ||||||
| 			id = userConfig.Alias | 			id = userConfig.Alias | ||||||
| 			if len(userConfig.ColorScheme) > 0 { | 			if len(userConfig.ColorScheme) > 0 { | ||||||
| 				colorScheme = userConfig.ColorScheme | 				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 ( | var ( | ||||||
| 	config Config | 	config Config | ||||||
| ) | ) | ||||||
| @@ -219,7 +202,7 @@ func main() { | |||||||
| 	configure(*configFile) | 	configure(*configFile) | ||||||
|  |  | ||||||
| 	router := mux.NewRouter() | 	router := mux.NewRouter() | ||||||
| 	router.HandleFunc("/avatar/{id}", RequestHandler) | 	router.HandleFunc("/avatar/{id}", requestHandler) | ||||||
| 	log.Println("Starting ...") | 	log.Println("Starting ...") | ||||||
| 	log.Fatal(http.ListenAndServe(":8000", router)) | 	log.Fatal(http.ListenAndServe(":8000", router)) | ||||||
| } | } | ||||||
|   | |||||||
| @@ -27,7 +27,7 @@ var gh2 []byte | |||||||
|  |  | ||||||
| func testRouter() *mux.Router { | func testRouter() *mux.Router { | ||||||
| 	router := mux.NewRouter() | 	router := mux.NewRouter() | ||||||
| 	router.HandleFunc("/avatar/{id}", RequestHandler) | 	router.HandleFunc("/avatar/{id}", requestHandler) | ||||||
| 	return router | 	return router | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -116,7 +116,7 @@ func TestUsesConfig(t *testing.T) { | |||||||
| 	configure("./testdata/testconfig.toml") | 	configure("./testdata/testconfig.toml") | ||||||
|  |  | ||||||
| 	if config.Defaults.ColorScheme != "gh" || | 	if config.Defaults.ColorScheme != "gh" || | ||||||
| 		config.Users[0].Id != "example" || | 		config.Users[0].ID != "example" || | ||||||
| 		config.Users[0].Alias != "42" || | 		config.Users[0].Alias != "42" || | ||||||
| 		config.Users[0].ColorScheme != "gh" || | 		config.Users[0].ColorScheme != "gh" || | ||||||
| 		config.Users[0].Pattern != "github" { | 		config.Users[0].Pattern != "github" { | ||||||
| @@ -131,7 +131,7 @@ func TestUsesConfigWithEnvVar(t *testing.T) { | |||||||
| 	configure("./testdata/testconfig.toml") | 	configure("./testdata/testconfig.toml") | ||||||
|  |  | ||||||
| 	if config.Defaults.ColorScheme != "v1" || | 	if config.Defaults.ColorScheme != "v1" || | ||||||
| 		config.Users[0].Id != "example" || | 		config.Users[0].ID != "example" || | ||||||
| 		config.Users[0].Alias != "42" || | 		config.Users[0].Alias != "42" || | ||||||
| 		config.Users[0].ColorScheme != "gh" || | 		config.Users[0].ColorScheme != "gh" || | ||||||
| 		config.Users[0].Pattern != "github" { | 		config.Users[0].Pattern != "github" { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user