feature(config): allow environment variable override

This commit is contained in:
wpetit 2020-07-17 11:45:35 +02:00
parent 5c5a51f9b1
commit 8982e1dd3f
2 changed files with 20 additions and 7 deletions

View File

@ -76,6 +76,10 @@ func main() {
} }
if err := config.WithEnvironment(conf); err != nil {
log.Fatalf("%+v", errors.Wrap(err, "could not override config with environment"))
}
// Dump configuration if asked // Dump configuration if asked
if dumpConfig { if dumpConfig {
if err := config.Dump(conf, os.Stdout); err != nil { if err := config.Dump(conf, os.Stdout); err != nil {

View File

@ -7,11 +7,12 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/caarlos0/env/v6"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
type Config struct { type Config struct {
Debug bool `yaml:"debug"` Debug bool `yaml:"debug" env:"DEBUG"`
HTTP HTTPConfig `yaml:"http"` HTTP HTTPConfig `yaml:"http"`
} }
@ -32,12 +33,12 @@ func NewFromFile(filepath string) (*Config, error) {
} }
type HTTPConfig struct { type HTTPConfig struct {
Address string `yaml:"address"` Address string `yaml:"address" env:"HTTP_ADDRESS"`
CookieAuthenticationKey string `yaml:"cookieAuthenticationKey"` CookieAuthenticationKey string `yaml:"cookieAuthenticationKey" env:"HTTP_COOKIE_AUTHENTICATION_KEY"`
CookieEncryptionKey string `yaml:"cookieEncryptionKey"` CookieEncryptionKey string `yaml:"cookieEncryptionKey" env:"HTTP_COOKIE_ENCRYPTION_KEY"`
CookieMaxAge int `yaml:"cookieMaxAge"` CookieMaxAge int `yaml:"cookieMaxAge" env:"HTTP_COOKIE_MAX_AGE"`
TemplateDir string `yaml:"templateDir"` TemplateDir string `yaml:"templateDir" env:"HTTP_TEMPLATE_DIR"`
PublicDir string `yaml:"publicDir"` PublicDir string `yaml:"publicDir" env:"HTTP_PUBLIC_DIR"`
} }
func NewDumpDefault() *Config { func NewDumpDefault() *Config {
@ -71,3 +72,11 @@ func Dump(config *Config, w io.Writer) error {
return nil return nil
} }
func WithEnvironment(conf *Config) error {
if err := env.Parse(conf); err != nil {
return err
}
return nil
}