2020-04-11 08:45:06 +02:00
|
|
|
package serv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-04-17 16:56:26 +02:00
|
|
|
"path/filepath"
|
2020-04-11 08:45:06 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadInConfig function reads in the config file for the environment specified in the GO_ENV
|
|
|
|
// environment variable. This is the best way to create a new Super Graph config.
|
|
|
|
func ReadInConfig(configFile string) (*Config, error) {
|
|
|
|
cpath := path.Dir(configFile)
|
|
|
|
cfile := path.Base(configFile)
|
|
|
|
vi := newViper(cpath, cfile)
|
|
|
|
|
|
|
|
if err := vi.ReadInConfig(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits := vi.GetString("inherits")
|
|
|
|
|
2020-05-17 09:11:56 +02:00
|
|
|
if inherits != "" {
|
2020-04-11 08:45:06 +02:00
|
|
|
vi = newViper(cpath, inherits)
|
|
|
|
|
|
|
|
if err := vi.ReadInConfig(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if vi.IsSet("inherits") {
|
|
|
|
return nil, fmt.Errorf("inherited config (%s) cannot itself inherit (%s)",
|
|
|
|
inherits,
|
|
|
|
vi.GetString("inherits"))
|
|
|
|
}
|
|
|
|
|
|
|
|
vi.SetConfigName(cfile)
|
|
|
|
|
|
|
|
if err := vi.MergeInConfig(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Config{cpath: cpath, vi: vi}
|
|
|
|
|
|
|
|
if err := vi.Unmarshal(&c); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode config, %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newViper(configPath, configFile string) *viper.Viper {
|
|
|
|
vi := viper.New()
|
|
|
|
|
|
|
|
vi.SetEnvPrefix("SG")
|
|
|
|
vi.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
vi.AutomaticEnv()
|
|
|
|
|
|
|
|
vi.AddConfigPath(configPath)
|
|
|
|
vi.SetConfigName(configFile)
|
|
|
|
vi.AddConfigPath("./config")
|
|
|
|
|
|
|
|
vi.SetDefault("host_port", "0.0.0.0:8080")
|
|
|
|
vi.SetDefault("web_ui", false)
|
|
|
|
vi.SetDefault("enable_tracing", false)
|
2020-06-07 19:02:57 +02:00
|
|
|
vi.SetDefault("auth_fail_block", false)
|
2020-04-11 08:45:06 +02:00
|
|
|
vi.SetDefault("seed_file", "seed.js")
|
|
|
|
|
2020-05-28 06:07:01 +02:00
|
|
|
vi.SetDefault("default_block", true)
|
|
|
|
|
2020-04-11 08:45:06 +02:00
|
|
|
vi.SetDefault("database.type", "postgres")
|
|
|
|
vi.SetDefault("database.host", "localhost")
|
|
|
|
vi.SetDefault("database.port", 5432)
|
|
|
|
vi.SetDefault("database.user", "postgres")
|
|
|
|
vi.SetDefault("database.schema", "public")
|
|
|
|
|
|
|
|
vi.SetDefault("env", "development")
|
|
|
|
|
|
|
|
vi.BindEnv("env", "GO_ENV") //nolint: errcheck
|
|
|
|
vi.BindEnv("host", "HOST") //nolint: errcheck
|
|
|
|
vi.BindEnv("port", "PORT") //nolint: errcheck
|
|
|
|
|
|
|
|
vi.SetDefault("auth.rails.max_idle", 80)
|
|
|
|
vi.SetDefault("auth.rails.max_active", 12000)
|
|
|
|
|
|
|
|
return vi
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetConfigName() string {
|
|
|
|
if len(os.Getenv("GO_ENV")) == 0 {
|
|
|
|
return "dev"
|
|
|
|
}
|
|
|
|
|
|
|
|
ge := strings.ToLower(os.Getenv("GO_ENV"))
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(ge, "pro"):
|
|
|
|
return "prod"
|
|
|
|
|
|
|
|
case strings.HasPrefix(ge, "sta"):
|
|
|
|
return "stage"
|
|
|
|
|
|
|
|
case strings.HasPrefix(ge, "tes"):
|
|
|
|
return "test"
|
|
|
|
|
|
|
|
case strings.HasPrefix(ge, "dev"):
|
|
|
|
return "dev"
|
|
|
|
}
|
|
|
|
|
|
|
|
return ge
|
|
|
|
}
|
2020-04-17 16:56:26 +02:00
|
|
|
|
2020-05-23 17:43:57 +02:00
|
|
|
func (c *Config) telemetryEnabled() bool {
|
2020-05-23 22:37:15 +02:00
|
|
|
return c.Telemetry.Debug || c.Telemetry.Metrics.Exporter != "" || c.Telemetry.Tracing.Exporter != ""
|
2020-05-23 17:43:57 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 16:56:26 +02:00
|
|
|
func (c *Config) relPath(p string) string {
|
|
|
|
if filepath.IsAbs(p) {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.Join(c.cpath, p)
|
|
|
|
}
|