Skip to content
Snippets Groups Projects
Commit a697aafb authored by Tobias Meisel's avatar Tobias Meisel
Browse files

update config (envconfig)

parent ddf9d6a6
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment