2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2018-06-09 16:27:47 +08:00
|
|
|
"path/filepath"
|
2018-08-17 01:42:21 +08:00
|
|
|
"strconv"
|
2018-06-11 01:43:42 +08:00
|
|
|
"strings"
|
2018-05-27 22:40:42 +08:00
|
|
|
)
|
|
|
|
|
2018-07-24 14:41:01 +08:00
|
|
|
func configParse() error {
|
2018-06-09 16:27:47 +08:00
|
|
|
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)
|
2018-06-09 16:27:47 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
defaults := map[string]string{
|
2018-06-21 23:21:10 +08:00
|
|
|
"CONFIG_FILE": "",
|
|
|
|
|
2018-06-08 03:09:27 +08:00
|
|
|
"POSTGRES": "postgres://postgres:postgres@localhost/commento?sslmode=disable",
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2018-08-17 01:42:21 +08:00
|
|
|
// PostgreSQL recommends max_connections in the order of hundreds. The default
|
|
|
|
// is 100, so let's use half that and leave the other half for other services.
|
|
|
|
// Ideally, you'd be setting this to a much higher number (for example, at the
|
|
|
|
// time of writing, commento.io uses 600). See https://wiki.postgresql.org/wiki/Number_Of_Database_Connections
|
|
|
|
"MAX_IDLE_PG_CONNECTIONS": "50",
|
|
|
|
|
2018-06-16 21:30:03 +08:00
|
|
|
"BIND_ADDRESS": "127.0.0.1",
|
|
|
|
"PORT": "8080",
|
|
|
|
"ORIGIN": "",
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
"CDN_PREFIX": "",
|
|
|
|
|
2018-07-24 18:08:00 +08:00
|
|
|
"FORBID_NEW_OWNERS": "false",
|
|
|
|
|
2018-06-09 16:27:47 +08:00
|
|
|
"STATIC": binPath,
|
|
|
|
|
2018-06-11 17:31:58 +08:00
|
|
|
"GZIP_STATIC": "false",
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
"SMTP_USERNAME": "",
|
|
|
|
"SMTP_PASSWORD": "",
|
|
|
|
"SMTP_HOST": "",
|
2018-06-09 17:52:49 +08:00
|
|
|
"SMTP_PORT": "",
|
2018-05-27 22:40:42 +08:00
|
|
|
"SMTP_FROM_ADDRESS": "",
|
|
|
|
|
2018-12-20 11:57:02 +08:00
|
|
|
"AKISMET_KEY": "",
|
|
|
|
|
2018-06-11 01:43:42 +08:00
|
|
|
"GOOGLE_KEY": "",
|
|
|
|
"GOOGLE_SECRET": "",
|
2019-01-31 10:15:16 +08:00
|
|
|
|
|
|
|
"GITHUB_KEY": "",
|
|
|
|
"GITHUB_SECRET": "",
|
2019-02-23 10:23:24 +08:00
|
|
|
|
|
|
|
"TWITTER_KEY": "",
|
|
|
|
"TWITTER_SECRET": "",
|
2019-02-23 10:54:08 +08:00
|
|
|
|
|
|
|
"GITLAB_KEY": "",
|
|
|
|
"GITLAB_SECRET": "",
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2019-08-22 12:07:44 +08:00
|
|
|
if os.Getenv("COMMENTO_CONFIG_FILE") != "" {
|
|
|
|
if err := configFileLoad(os.Getenv("COMMENTO_CONFIG_FILE")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
for key, value := range defaults {
|
2018-06-11 01:43:42 +08:00
|
|
|
if os.Getenv("COMMENTO_"+key) == "" {
|
2018-05-27 22:40:42 +08:00
|
|
|
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))
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mandatory config parameters
|
2018-08-17 01:42:21 +08:00
|
|
|
for _, env := range []string{"POSTGRES", "PORT", "ORIGIN", "FORBID_NEW_OWNERS", "MAX_IDLE_PG_CONNECTIONS"} {
|
2018-05-27 22:40:42 +08:00
|
|
|
if os.Getenv(env) == "" {
|
2018-06-13 02:52:06 +08:00
|
|
|
logger.Errorf("missing COMMENTO_%s environment variable", env)
|
2018-05-27 22:40:42 +08:00
|
|
|
return errorMissingConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 13:17:20 +08:00
|
|
|
os.Setenv("ORIGIN", strings.TrimSuffix(os.Getenv("ORIGIN"), "/"))
|
2018-08-13 01:08:48 +08:00
|
|
|
os.Setenv("ORIGIN", addHttpIfAbsent(os.Getenv("ORIGIN")))
|
2018-08-08 13:17:20 +08:00
|
|
|
|
2018-06-03 23:17:31 +08:00
|
|
|
if os.Getenv("CDN_PREFIX") == "" {
|
|
|
|
os.Setenv("CDN_PREFIX", os.Getenv("ORIGIN"))
|
|
|
|
}
|
|
|
|
|
2018-08-13 01:09:06 +08:00
|
|
|
os.Setenv("CDN_PREFIX", strings.TrimSuffix(os.Getenv("CDN_PREFIX"), "/"))
|
2018-08-13 01:08:48 +08:00
|
|
|
os.Setenv("CDN_PREFIX", addHttpIfAbsent(os.Getenv("CDN_PREFIX")))
|
2018-08-08 13:17:20 +08:00
|
|
|
|
2018-07-24 18:08:00 +08:00
|
|
|
if os.Getenv("FORBID_NEW_OWNERS") != "true" && os.Getenv("FORBID_NEW_OWNERS") != "false" {
|
|
|
|
logger.Errorf("COMMENTO_FORBID_NEW_OWNERS neither 'true' nor 'false'")
|
|
|
|
return errorInvalidConfigValue
|
|
|
|
}
|
|
|
|
|
2018-06-09 16:27:47 +08:00
|
|
|
static := os.Getenv("STATIC")
|
|
|
|
for strings.HasSuffix(static, "/") {
|
2018-06-11 01:43:42 +08:00
|
|
|
static = static[0 : len(static)-1]
|
2018-06-09 16:27:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-08-17 01:42:21 +08:00
|
|
|
if num, err := strconv.Atoi(os.Getenv("MAX_IDLE_PG_CONNECTIONS")); err != nil {
|
|
|
|
logger.Errorf("invalid COMMENTO_MAX_IDLE_PG_CONNECTIONS: %v", err)
|
|
|
|
return errorInvalidConfigValue
|
|
|
|
} else if num <= 0 {
|
|
|
|
logger.Errorf("COMMENTO_MAX_IDLE_PG_CONNECTIONS should be a positive integer")
|
|
|
|
return errorInvalidConfigValue
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
return nil
|
|
|
|
}
|