-
Notifications
You must be signed in to change notification settings - Fork 0
ghkdqhrbals:viper
Hwangbo Gyumin edited this page May 29, 2022
·
1 revision
viper을 통해 configuration 설정
/app.env configuration 구성
DB_DRIVER=postgres
DB_SOURCE=postgresql://root:secret@localhost:5432/simple_bank?sslmode=disable
SERVER_ADDRESS=0.0.0.0:8080
/util/config.go 구성
// JSON to Config
type Config struct {
DBDriver string `mapstructure:"DB_DRIVER"`
DBSource string `mapstructure:"DB_SOURCE"`
ServerAddress string `mapstructure:"SERVER_ADDRESS"`
}
func LoadConfig(path string) (config Config, err error) {
viper.AddConfigPath(path)
viper.SetConfigName("app")
viper.SetConfigType("env")
viper.AutomaticEnv()
err = viper.ReadInConfig()
if err != nil {
return
}
err = viper.Unmarshal(&config)
return
}
/main.go config 자동설정 구성
config, err := util.LoadConfig(".")
if err != nil {
log.Fatal("cannot load config:", err)
}
This project provide basic banking service setup with Go