package app import ( "path/filepath" "strings" "github.com/spf13/viper" ) var ( Config appConfig ) type appConfig struct { DB struct { Host string `mapstructure:"host"` Port string `mapstructure:"port"` User string `mapstructure:"user"` Password string `mapstructure:"password"` Name string `mapstructure:"name"` MaxIdleConnections int `mapstructure:"max_idle_connections"` MaxOpenConnections int `mapstructure:"max_idle_connections"` } `mapstructure:"db"` Secret struct { AuthKey string `mapstructure:"auth_key"` } `mapstructure:"secret"` } func InitConfig(cfg string) { viper.SetConfigFile(cfg) viper.SetConfigType(strings.Trim(filepath.Ext(cfg), ".")) if err := viper.ReadInConfig(); err != nil { panic(err) } if err := viper.Unmarshal(&Config); err != nil { panic(err) } }