diff --git a/api/config.go b/api/config.go index 5952868..874ca45 100644 --- a/api/config.go +++ b/api/config.go @@ -14,6 +14,8 @@ func parseConfig() error { } defaults := map[string]string{ + "CONFIG_FILE": "", + "POSTGRES": "postgres://postgres:postgres@localhost/commento?sslmode=disable", "BIND_ADDRESS": "127.0.0.1", @@ -36,6 +38,12 @@ func parseConfig() error { "GOOGLE_SECRET": "", } + if os.Getenv("COMMENTO_CONFIG_FILE") != "" { + if err := configFileLoad(os.Getenv("COMMENTO_CONFIG_FILE")); err != nil { + return err + } + } + for key, value := range defaults { if os.Getenv("COMMENTO_"+key) == "" { os.Setenv(key, value) diff --git a/api/config_file.go b/api/config_file.go new file mode 100644 index 0000000..5e4c013 --- /dev/null +++ b/api/config_file.go @@ -0,0 +1,37 @@ +package main + +import ( + "bufio" + "os" + "strings" +) + +func configFileLoad(filepath string) error { + file, err := os.Open(filepath) + if err != nil { + return err + } + + defer file.Close() + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + + i := strings.Index(line, "=") + if i == -1 { + continue + } + + key := line[:i] + value := line[i+1:] + + if !strings.HasPrefix(key, "COMMENTO_") { + continue + } + + os.Setenv(key[9:], value) + } + + return nil +}