2021-05-02 23:49:16 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2021-05-04 13:07:12 +01:00
|
|
|
"io/ioutil"
|
|
|
|
|
2021-07-29 10:30:30 +01:00
|
|
|
env "github.com/Netflix/go-env"
|
2021-05-04 13:07:12 +01:00
|
|
|
"github.com/imdario/mergo"
|
|
|
|
"github.com/pkg/errors"
|
2021-05-02 23:49:16 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-05-04 13:07:12 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
2021-05-02 23:49:16 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var G Config
|
|
|
|
|
|
|
|
func DefaultConfig() {
|
|
|
|
G = Config{
|
2021-05-04 13:07:12 +01:00
|
|
|
Debug: false,
|
2021-05-02 23:49:16 +01:00
|
|
|
Web: WebConfig{
|
2021-12-11 21:02:40 +00:00
|
|
|
Listen: "localhost:9000",
|
|
|
|
ListenTLS: "localhost:9443",
|
2021-05-02 23:49:16 +01:00
|
|
|
},
|
|
|
|
Paths: PathsConfig{
|
|
|
|
Media: "./media",
|
|
|
|
},
|
2021-05-04 13:07:12 +01:00
|
|
|
LogLevel: "info",
|
2021-05-04 13:28:48 +01:00
|
|
|
ErrorReporting: ErrorReportingConfig{
|
|
|
|
Enabled: false,
|
2021-11-02 13:44:37 +00:00
|
|
|
DSN: "https://a579bb09306d4f8b8d8847c052d3a1d3@sentry.beryju.org/8",
|
2021-05-04 13:28:48 +01:00
|
|
|
},
|
2021-05-04 13:07:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(path string) error {
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to load config file")
|
2021-05-02 23:49:16 +01:00
|
|
|
}
|
2021-05-04 13:07:12 +01:00
|
|
|
nc := Config{}
|
2021-07-29 10:30:30 +01:00
|
|
|
err = yaml.Unmarshal(raw, &nc)
|
2021-05-04 13:07:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to parse YAML")
|
|
|
|
}
|
|
|
|
if err := mergo.Merge(&G, nc, mergo.WithOverride); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to overlay config")
|
|
|
|
}
|
|
|
|
log.WithField("path", path).Debug("Loaded config")
|
|
|
|
return nil
|
2021-05-02 23:49:16 +01:00
|
|
|
}
|
|
|
|
|
2021-07-29 10:30:30 +01:00
|
|
|
func FromEnv() error {
|
|
|
|
nc := Config{}
|
|
|
|
_, err := env.UnmarshalFromEnviron(&nc)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to load environment variables")
|
|
|
|
}
|
|
|
|
if err := mergo.Merge(&G, nc, mergo.WithOverride); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to overlay config")
|
|
|
|
}
|
|
|
|
log.Debug("Loaded config from environment")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-02 23:49:16 +01:00
|
|
|
func ConfigureLogger() {
|
2021-05-04 13:07:12 +01:00
|
|
|
switch G.LogLevel {
|
2021-05-02 23:49:16 +01:00
|
|
|
case "trace":
|
|
|
|
log.SetLevel(log.TraceLevel)
|
|
|
|
case "debug":
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
case "info":
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
case "warning":
|
|
|
|
log.SetLevel(log.WarnLevel)
|
|
|
|
case "error":
|
|
|
|
log.SetLevel(log.ErrorLevel)
|
|
|
|
default:
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2021-05-04 13:07:12 +01:00
|
|
|
if G.Debug {
|
|
|
|
log.SetFormatter(&log.TextFormatter{})
|
|
|
|
} else {
|
2021-05-02 23:49:16 +01:00
|
|
|
log.SetFormatter(&log.JSONFormatter{
|
|
|
|
FieldMap: log.FieldMap{
|
|
|
|
log.FieldKeyMsg: "event",
|
|
|
|
log.FieldKeyTime: "timestamp",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|