Skip to content
Snippets Groups Projects
Commit a076069d authored by Steffen Schulze's avatar Steffen Schulze
Browse files

Merge branch 'config-update' into 'main'

update config (envconfig)

See merge request eclipse/xfsc/libraries/microservice/core!10
parents ddf9d6a6 a697aafb
No related branches found
No related tags found
No related merge requests found
...@@ -13,9 +13,10 @@ var viperInstance = viper.New() ...@@ -13,9 +13,10 @@ var viperInstance = viper.New()
// BaseConfig can be used to import the base config parameters in the applications config struct. // BaseConfig can be used to import the base config parameters in the applications config struct.
// Please use with tag `mapstructure:",squash"`. // Please use with tag `mapstructure:",squash"`.
type BaseConfig struct { type BaseConfig struct {
LogLevel string `mapstructure:"logLevel"` LogLevel string `mapstructure:"logLevel" envconfig:"LOG_LEVEL" default:"info"`
IsDev bool `mapstructure:"isDev"` IsDev bool `mapstructure:"isDev" envconfig:"IS_DEV" default:"false"`
Port int `mapstructure:"servingPort"` 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. // LoadConfig sets given defaults and read in given config.
...@@ -41,19 +42,21 @@ func readConfig(prefix string) error { ...@@ -41,19 +42,21 @@ func readConfig(prefix string) error {
viperInstance.SetEnvPrefix(strings.ToTitle(prefix)) viperInstance.SetEnvPrefix(strings.ToTitle(prefix))
viperInstance.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viperInstance.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viperInstance.AutomaticEnv()
if err := viperInstance.ReadInConfig(); err != nil { if err := viperInstance.ReadInConfig(); err != nil {
var configFileNotFoundError viper.ConfigFileNotFoundError if !errors.Is(err, viper.ConfigFileNotFoundError{}) {
if !errors.As(err, &configFileNotFoundError) {
return fmt.Errorf("error read in configFile: %w", err) return fmt.Errorf("error read in configFile: %w", err)
} }
} }
viperInstance.AutomaticEnv()
return nil return nil
} }
func setDefaults(defaults map[string]any) { func setDefaults(defaults map[string]any) {
viperInstance.SetDefault("logLevel", "info") viperInstance.SetDefault("logLevel", "info")
viperInstance.SetDefault("servingPort", 8080) viperInstance.SetDefault("listenAddr", "127.0.0.1")
viperInstance.SetDefault("listenPort", 8080)
for key, value := range defaults { for key, value := range defaults {
viperInstance.SetDefault(key, value) 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