diff --git a/pkg/config/config.go b/pkg/config/config.go index 5772c16ffbbb5166483d9840c9b789656f0f94ab..397fe9defb9415ae026951f8d4c63523d99792e6 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -13,9 +13,10 @@ var viperInstance = viper.New() // BaseConfig can be used to import the base config parameters in the applications config struct. // Please use with tag `mapstructure:",squash"`. type BaseConfig struct { - LogLevel string `mapstructure:"logLevel"` - IsDev bool `mapstructure:"isDev"` - Port int `mapstructure:"servingPort"` + LogLevel string `mapstructure:"logLevel" envconfig:"LOG_LEVEL" default:"info"` + IsDev bool `mapstructure:"isDev" envconfig:"IS_DEV" default:"false"` + ListenAddr string `mapstructure:"listenAddr" envconfig:"LISTEN_ADDR" default:"127.0.0.1"` + ListenPort int `mapstructure:"listenPort" envconfig:"LISTEN_PORT" default:"8080"` } // LoadConfig sets given defaults and read in given config. @@ -41,19 +42,21 @@ func readConfig(prefix string) error { viperInstance.SetEnvPrefix(strings.ToTitle(prefix)) viperInstance.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) + viperInstance.AutomaticEnv() + if err := viperInstance.ReadInConfig(); err != nil { - var configFileNotFoundError viper.ConfigFileNotFoundError - if !errors.As(err, &configFileNotFoundError) { + if !errors.Is(err, viper.ConfigFileNotFoundError{}) { return fmt.Errorf("error read in configFile: %w", err) } } - viperInstance.AutomaticEnv() + return nil } func setDefaults(defaults map[string]any) { viperInstance.SetDefault("logLevel", "info") - viperInstance.SetDefault("servingPort", 8080) + viperInstance.SetDefault("listenAddr", "127.0.0.1") + viperInstance.SetDefault("listenPort", 8080) for key, value := range defaults { viperInstance.SetDefault(key, value)