Created
May 16, 2021 18:39
-
-
Save madflojo/fa093baf85c77ad188ca96fb9edf8be3 to your computer and use it in GitHub Desktop.
Viper Article - Loading Config from Consul
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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