123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package global
- import (
- "fmt"
- "github.com/fsnotify/fsnotify"
- "github.com/go-redis/redis/v8"
- oplogging "github.com/op/go-logging"
- "github.com/rdlucklib/rdluck_tools/cache"
- "github.com/spf13/viper"
- "gorm.io/gorm"
- "hongze/mysteel_watch/config"
- "hongze/mysteel_watch/utils"
- )
- var (
- CONFIG config.Config
- LOG *oplogging.Logger
-
-
- DEFAULT_MYSQL *gorm.DB
- Redis *redis.Client
-
- Rc *cache.Cache
- Re error
- )
- const ConfigFile = "config/config.yaml"
- const ProConfigFile = "config/config.yaml"
- func init() {
- v := viper.New()
- configFilePath := ConfigFile
-
- if !utils.FileIsExist(configFilePath) {
- configFilePath = ProConfigFile
- }
- fmt.Println("configFilePath->", configFilePath)
-
- 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)
- }
- }
|