12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 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/qiniu/qmgo"
- "github.com/spf13/viper"
- "gorm.io/gorm"
- "hongze/hongze_yb/config"
- "hongze/hongze_yb/utils"
- "io"
- )
- var (
- CONFIG config.Config
- LOG *oplogging.Logger
- FILE_LOG *oplogging.Logger
- MONGO_LOG *oplogging.Logger
- MYSQL map[string]*gorm.DB
- MYSQL_LOG io.Writer
- DEFAULT_MYSQL *gorm.DB
- Redis *redis.Client
- EsClient *elastic.Client
- MgoDataCli *qmgo.Client
- )
- const ConfigFile = "config/config.yaml"
- const ProConfigFile = "/home/code/config/hongze_yb/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)
- }
- }
|