代码拉取完成,页面将自动刷新
package settings
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
var Conf = new(AppConfig) // 从该对象访问数据
type AppConfig struct {
Mode string `mapstructure:"mode"`
Port int `mapstructure:"port"`
*LogConfig `mapstructure:"log"`
*MySQLConfig `mapstructure:"mysql"`
*RedisConfig `mapstructure:"redis"`
}
type MySQLConfig struct {
Host string `mapstructure:"host"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DB string `mapstructure:"db"`
Port int `mapstructure:"port"`
MaxOpenConns int `mapstructure:"max_open_conns"`
MaxIdleConns int `mapstructure:"max_idle_conns"`
}
type RedisConfig struct {
Host string `mapstructure:"host"`
Password string `mapstructure:"password"`
Port int `mapstructure:"port"`
DB int `mapstructure:"db"`
PoolSize int `mapstructure:"pool_size"`
MinIdleConns int `mapstructure:"min_idle_conns"`
}
type LogConfig struct {
Level string `mapstructure:"level"`
Filename string `mapstructure:"filename"`
MaxSize int `mapstructure:"max_size"`
MaxAge int `mapstructure:"max_age"`
MaxBackups int `mapstructure:"max_backups"`
}
// Init mapstructure的作用:
// 正常处理过程是将json/yaml等二进制数据转化成map之后,再转化成struct对象。
// 通过mapstructure可以直接将json/yaml等二进制数据转化成struct对象。
func Init() error {
viper.SetConfigFile("./conf/config.yaml")
viper.WatchConfig() // 监控配置文件变化
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("配置文件变化:", e.Name)
viper.Unmarshal(&Conf)
})
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("ReadInConfig failed, err: %v", err))
}
if err := viper.Unmarshal(&Conf); err != nil {
panic(fmt.Errorf("unmarshal failed, err: %v", err))
}
return err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。