-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example): Allow configuring some parameters with env variables
- Loading branch information
Andrey Rusakov
committed
Oct 9, 2024
1 parent
5ae555e
commit 0410628
Showing
6 changed files
with
262 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
const ( | ||
// default port for the http server to run | ||
DefaultIssuerPort = "9998" | ||
) | ||
|
||
type Config struct { | ||
Port string | ||
RedirectURIs []string | ||
UsersFile string | ||
} | ||
|
||
// FromEnvVars loads configuration parameters from environment variables. | ||
// If there is no such variable defined, then use default values. | ||
func FromEnvVars(defaults *Config) *Config { | ||
if defaults == nil { | ||
defaults = &Config{} | ||
} | ||
cfg := &Config{ | ||
Port: defaults.Port, | ||
RedirectURIs: defaults.RedirectURIs, | ||
UsersFile: defaults.UsersFile, | ||
} | ||
if value, ok := os.LookupEnv("PORT"); ok { | ||
cfg.Port = value | ||
} | ||
if value, ok := os.LookupEnv("USERS_FILE"); ok { | ||
cfg.UsersFile = value | ||
} | ||
if value, ok := os.LookupEnv("REDIRECT_URIS"); ok { | ||
cfg.RedirectURIs = strings.Split(value, ",") | ||
} | ||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestFromEnvVars(t *testing.T) { | ||
|
||
for _, tc := range []struct { | ||
name string | ||
env map[string]string | ||
defaults *Config | ||
want *Config | ||
}{ | ||
{ | ||
name: "no vars, no default values", | ||
env: map[string]string{}, | ||
want: &Config{}, | ||
}, | ||
{ | ||
name: "no vars, only defaults", | ||
env: map[string]string{}, | ||
defaults: &Config{ | ||
Port: "6666", | ||
UsersFile: "/default/user/path", | ||
RedirectURIs: []string{"re", "direct", "uris"}, | ||
}, | ||
want: &Config{ | ||
Port: "6666", | ||
UsersFile: "/default/user/path", | ||
RedirectURIs: []string{"re", "direct", "uris"}, | ||
}, | ||
}, | ||
{ | ||
name: "overriding default values", | ||
env: map[string]string{ | ||
"PORT": "1234", | ||
"USERS_FILE": "/path/to/users", | ||
"REDIRECT_URIS": "http://redirect/redirect", | ||
}, | ||
defaults: &Config{ | ||
Port: "6666", | ||
UsersFile: "/default/user/path", | ||
RedirectURIs: []string{"re", "direct", "uris"}, | ||
}, | ||
want: &Config{ | ||
Port: "1234", | ||
UsersFile: "/path/to/users", | ||
RedirectURIs: []string{"http://redirect/redirect"}, | ||
}, | ||
}, | ||
{ | ||
name: "multiple redirect uris", | ||
env: map[string]string{ | ||
"REDIRECT_URIS": "http://host_1,http://host_2,http://host_3", | ||
}, | ||
want: &Config{ | ||
RedirectURIs: []string{ | ||
"http://host_1", "http://host_2", "http://host_3", | ||
}, | ||
}, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
os.Clearenv() | ||
for k, v := range tc.env { | ||
os.Setenv(k, v) | ||
} | ||
cfg := FromEnvVars(tc.defaults) | ||
if fmt.Sprint(cfg) != fmt.Sprint(tc.want) { | ||
t.Errorf("Expected FromEnvVars()=%q, but got %q", tc.want, cfg) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package storage | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"reflect" | ||
"testing" | ||
|
||
"golang.org/x/text/language" | ||
) | ||
|
||
func TestStoreFromFile(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
pathToFile string | ||
content string | ||
want UserStore | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "normal user file", | ||
pathToFile: "userfile.json", | ||
content: `{ | ||
"id1": { | ||
"ID": "id1", | ||
"EmailVerified": true, | ||
"PreferredLanguage": "DE" | ||
} | ||
}`, | ||
want: userStore{map[string]*User{ | ||
"id1": { | ||
ID: "id1", | ||
EmailVerified: true, | ||
PreferredLanguage: language.German, | ||
}, | ||
}}, | ||
}, | ||
{ | ||
name: "malformed file", | ||
pathToFile: "whatever", | ||
content: "not a json just a text", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "not existing file", | ||
pathToFile: "what/ever/file", | ||
wantErr: true, | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actualPath := path.Join(t.TempDir(), tc.pathToFile) | ||
|
||
if tc.content != "" && tc.pathToFile != "" { | ||
if err := os.WriteFile(actualPath, []byte(tc.content), 0666); err != nil { | ||
t.Fatalf("cannot create file with test content: %q", tc.content) | ||
} | ||
} | ||
result, err := StoreFromFile(actualPath) | ||
if err != nil && !tc.wantErr { | ||
t.Errorf("StoreFromFile(%q) returned unexpected error %q", tc.pathToFile, err) | ||
} else if err == nil && tc.wantErr { | ||
t.Errorf("StoreFromFile(%q) did not return an expected error", tc.pathToFile) | ||
} | ||
if !tc.wantErr && !reflect.DeepEqual(tc.want, result.(userStore)) { | ||
t.Errorf("expected StoreFromFile(%q) = %v, but got %v", | ||
tc.pathToFile, tc.want, result) | ||
} | ||
}) | ||
} | ||
} |