package config import ( "log" "os" "strings" "github.com/spf13/viper" ) func ConfigDir() string { env := os.Getenv("CONFIG_PATH") if env != "" { return env } return "." } func Init() { // Read global config viper.SetConfigName("config") viper.SetConfigType("yaml") viper.AddConfigPath(ConfigDir()) // Bind ENV viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.AutomaticEnv() if err := viper.ReadInConfig(); err != nil { if _, ok := err.(viper.ConfigFileNotFoundError); ok { log.Println("[Config] No config file found, using Env vars only.") } else { log.Fatalf("[Config] Fatal error reading config file: %s \n", err) } } conf := &config{} if err := viper.Unmarshal(conf); err != nil { log.Fatalln("[Condig] Can't unmarshal config!") } }