Skip to content

Instantly share code, notes, and snippets.

@madflojo
Created May 16, 2021 18:39
Show Gist options
  • Save madflojo/fa093baf85c77ad188ca96fb9edf8be3 to your computer and use it in GitHub Desktop.
Save madflojo/fa093baf85c77ad188ca96fb9edf8be3 to your computer and use it in GitHub Desktop.
Viper Article - Loading Config from Consul
// Load Config
cfg.AddConfigPath("./conf")
cfg.SetEnvPrefix("app")
cfg.AllowEmptyEnv(true)
cfg.AutomaticEnv()
err := cfg.ReadInConfig()
if err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
log.Warnf("No Config file found, loaded config from Environment - Default path ./conf")
default:
log.Fatalf("Error when Fetching Configuration - %s", err)
}
}
// Load Config from Consul
if cfg.GetBool("use_consul") {
cfg.AddRemoteProvider("consul", cfg.GetString("consul_addr"), cfg.GetString("consul_keys_prefix"))
cfg.SetConfigType("json")
err = cfg.ReadRemoteConfig()
if err != nil {
log.Fatalf("Error when Fetching Configuration from Consul - %s", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment