commento/api/config.go

87 lines
1.7 KiB
Go
Raw Normal View History

package main
import (
"os"
"path/filepath"
2018-06-11 01:43:42 +08:00
"strings"
)
func parseConfig() error {
binPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
2018-06-09 16:32:07 +08:00
logger.Errorf("cannot load binary path: %v", err)
return err
}
defaults := map[string]string{
"CONFIG_FILE": "",
2018-06-08 03:09:27 +08:00
"POSTGRES": "postgres://postgres:postgres@localhost/commento?sslmode=disable",
2018-06-16 21:30:03 +08:00
"BIND_ADDRESS": "127.0.0.1",
"PORT": "8080",
"ORIGIN": "",
"CDN_PREFIX": "",
"STATIC": binPath,
2018-06-11 17:31:58 +08:00
"GZIP_STATIC": "false",
"SMTP_USERNAME": "",
"SMTP_PASSWORD": "",
"SMTP_HOST": "",
2018-06-09 17:52:49 +08:00
"SMTP_PORT": "",
"SMTP_FROM_ADDRESS": "",
2018-06-11 01:43:42 +08:00
"GOOGLE_KEY": "",
"GOOGLE_SECRET": "",
}
for key, value := range defaults {
2018-06-11 01:43:42 +08:00
if os.Getenv("COMMENTO_"+key) == "" {
os.Setenv(key, value)
2018-06-07 16:33:17 +08:00
} else {
2018-06-11 01:43:42 +08:00
os.Setenv(key, os.Getenv("COMMENTO_"+key))
}
}
if os.Getenv("CONFIG_FILE") != "" {
if err := configFileLoad(os.Getenv("CONFIG_FILE")); err != nil {
return err
}
}
// Mandatory config parameters
for _, env := range []string{"POSTGRES", "PORT", "ORIGIN"} {
if os.Getenv(env) == "" {
logger.Errorf("missing COMMENTO_%s environment variable", env)
return errorMissingConfig
}
}
if os.Getenv("CDN_PREFIX") == "" {
os.Setenv("CDN_PREFIX", os.Getenv("ORIGIN"))
}
static := os.Getenv("STATIC")
for strings.HasSuffix(static, "/") {
2018-06-11 01:43:42 +08:00
static = static[0 : len(static)-1]
}
file, err := os.Stat(static)
if err != nil {
logger.Errorf("cannot load %s: %v", static, err)
return err
}
if !file.IsDir() {
logger.Errorf("COMMENTO_STATIC=%s is not a directory", static)
return errorNotADirectory
}
os.Setenv("STATIC", static)
return nil
}