api: allow configuration files

Closes https://gitlab.com/commento/commento-ce/issues/55
This commit is contained in:
Adhityaa 2018-06-21 20:51:10 +05:30
parent 89852ef76a
commit 1b1c97f0f0
2 changed files with 45 additions and 0 deletions

View File

@ -14,6 +14,8 @@ func parseConfig() error {
} }
defaults := map[string]string{ defaults := map[string]string{
"CONFIG_FILE": "",
"POSTGRES": "postgres://postgres:postgres@localhost/commento?sslmode=disable", "POSTGRES": "postgres://postgres:postgres@localhost/commento?sslmode=disable",
"BIND_ADDRESS": "127.0.0.1", "BIND_ADDRESS": "127.0.0.1",
@ -36,6 +38,12 @@ func parseConfig() error {
"GOOGLE_SECRET": "", "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 { for key, value := range defaults {
if os.Getenv("COMMENTO_"+key) == "" { if os.Getenv("COMMENTO_"+key) == "" {
os.Setenv(key, value) os.Setenv(key, value)

37
api/config_file.go Normal file
View File

@ -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
}