config.go: parse config file before default

Closes https://gitlab.com/commento/commento/issues/187
This commit is contained in:
Adhityaa Chandrasekar 2019-08-21 21:07:44 -07:00
parent 9538c9036e
commit 52f8df5183
4 changed files with 15 additions and 13 deletions

View File

@ -58,6 +58,12 @@ func configParse() error {
"GITLAB_SECRET": "", "GITLAB_SECRET": "",
} }
if os.Getenv("COMMENTO_CONFIG_FILE") != "" {
if err := configFileLoad(os.Getenv("COMMENTO_CONFIG_FILE")); err != nil {
return err
}
}
for key, value := range defaults { for key, value := range defaults {
if os.Getenv("COMMENTO_"+key) == "" { if os.Getenv("COMMENTO_"+key) == "" {
os.Setenv(key, value) os.Setenv(key, value)
@ -66,12 +72,6 @@ func configParse() error {
} }
} }
if os.Getenv("CONFIG_FILE") != "" {
if err := configFileLoad(os.Getenv("CONFIG_FILE")); err != nil {
return err
}
}
// Mandatory config parameters // Mandatory config parameters
for _, env := range []string{"POSTGRES", "PORT", "ORIGIN", "FORBID_NEW_OWNERS", "MAX_IDLE_PG_CONNECTIONS"} { for _, env := range []string{"POSTGRES", "PORT", "ORIGIN", "FORBID_NEW_OWNERS", "MAX_IDLE_PG_CONNECTIONS"} {
if os.Getenv(env) == "" { if os.Getenv(env) == "" {

View File

@ -41,11 +41,12 @@ func configFileLoad(filepath string) error {
continue continue
} }
if os.Getenv(key[9:]) != "" { if os.Getenv(key) != "" {
// Config files have lower precedence.
continue continue
} }
os.Setenv(key[9:], value) os.Setenv(key, value)
} }
return nil return nil

View File

@ -37,19 +37,19 @@ func TestConfigFileLoadBasics(t *testing.T) {
return return
} }
os.Setenv("PORT", "9000") os.Setenv("COMMENTO_PORT", "9000")
if err := configFileLoad(f.Name()); err != nil { if err := configFileLoad(f.Name()); err != nil {
t.Errorf("unexpected error loading config file: %v", err) t.Errorf("unexpected error loading config file: %v", err)
return return
} }
if os.Getenv("PORT") != "9000" { if os.Getenv("COMMENTO_PORT") != "9000" {
t.Errorf("expected PORT=9000 got PORT=%s", os.Getenv("PORT")) t.Errorf("expected COMMENTO_PORT=9000 got COMMENTO_PORT=%s", os.Getenv("COMMENTO_PORT"))
return return
} }
if os.Getenv("GZIP_STATIC") != "true" { if os.Getenv("COMMENTO_GZIP_STATIC") != "true" {
t.Errorf("expected GZIP_STATIC=true got GZIP_STATIC=%s", os.Getenv("GZIP_STATIC")) t.Errorf("expected COMMENTO_GZIP_STATIC=true got COMMENTO_GZIP_STATIC=%s", os.Getenv("COMMENTO_GZIP_STATIC"))
return return
} }
} }

View File

@ -21,6 +21,7 @@ func TestConfigParseBasics(t *testing.T) {
os.Setenv("COMMENTO_BIND_ADDRESS", "192.168.1.100") os.Setenv("COMMENTO_BIND_ADDRESS", "192.168.1.100")
os.Setenv("COMMENTO_PORT", "")
if err := configParse(); err != nil { if err := configParse(); err != nil {
t.Errorf("unexpected error when parsing config: %v", err) t.Errorf("unexpected error when parsing config: %v", err)
return return