package global import ( "fmt" "github.com/fsnotify/fsnotify" "github.com/go-redis/redis/v8" "github.com/olivere/elastic/v7" oplogging "github.com/op/go-logging" "github.com/spf13/viper" "gorm.io/gorm" "hongze/hrms_api/config" "hongze/hrms_api/utils" "io" ) var ( CONFIG config.Config //配置文件 LOG *oplogging.Logger MYSQL map[string]*gorm.DB //数据库连接配置 MYSQL_LOG io.Writer DEFAULT_MYSQL *gorm.DB //默认数据库连接配置 Redis *redis.Client //redis链接 EsClient *elastic.Client ) const ConfigFile = "config/config.yaml" //本地(测试)环境下的配置文件地址 const ProConfigFile = "/home/code/config/hrms_api/config/config.yaml" //生产环境下的配置文件地址 func init() { v := viper.New() configFilePath := ConfigFile //如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径 if !utils.FileIsExist(configFilePath) { configFilePath = ProConfigFile } //设置配置文件 v.SetConfigFile(configFilePath) err := v.ReadInConfig() if err != nil { panic(fmt.Errorf("读取配置失败,Err: %s \n", err)) } //持续监听文件 v.WatchConfig() v.OnConfigChange(func(e fsnotify.Event) { fmt.Println("配置文件变更:", e.Name) if err := v.Unmarshal(&CONFIG); err != nil { fmt.Println("配置重赋值失败,Err:", err) } fmt.Println(CONFIG) }) if err := v.Unmarshal(&CONFIG); err != nil { fmt.Println("配置初始化赋值失败,Err:", err) } }