global.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package global
  2. import (
  3. "eta/eta_menu_sync/config"
  4. "eta/eta_menu_sync/utils"
  5. "fmt"
  6. "github.com/fsnotify/fsnotify"
  7. "github.com/go-redis/redis/v8"
  8. socketio "github.com/googollee/go-socket.io"
  9. "github.com/olivere/elastic/v7"
  10. oplogging "github.com/op/go-logging"
  11. "github.com/spf13/viper"
  12. "gorm.io/gorm"
  13. "io"
  14. "github.com/rdlucklib/rdluck_tools/cache"
  15. )
  16. var (
  17. CONFIG config.Config //配置文件
  18. LOG *oplogging.Logger
  19. MYSQL map[string]*gorm.DB //数据库连接配置
  20. MYSQL_LOG io.Writer
  21. DEFAULT_MYSQL *gorm.DB //默认数据库连接配置
  22. Redis *redis.Client //redis链接
  23. EsClient *elastic.Client
  24. Rc *cache.Cache //redis缓存
  25. Re error //redis错误
  26. Ws *socketio.Server //websocket的链接
  27. )
  28. // const ConfigFile = "config/config_debug.yaml" //本地(测试)环境下的配置文件地址
  29. const ConfigFile = "config/config.yaml" //本地(测试)环境下的配置文件地址
  30. const ProConfigFile = "config/config.yaml" //生产环境下的配置文件地址
  31. func init() {
  32. v := viper.New()
  33. configFilePath := ConfigFile
  34. //如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
  35. if !utils.FileIsExist(configFilePath) {
  36. configFilePath = ProConfigFile
  37. }
  38. fmt.Println("configFilePath->", configFilePath)
  39. //设置配置文件
  40. v.SetConfigFile(configFilePath)
  41. err := v.ReadInConfig()
  42. if err != nil {
  43. panic(any(fmt.Errorf("读取配置失败,Err: %s \n", err)))
  44. }
  45. //持续监听文件
  46. v.WatchConfig()
  47. v.OnConfigChange(func(e fsnotify.Event) {
  48. fmt.Println("配置文件变更:", e.Name)
  49. if err := v.Unmarshal(&CONFIG); err != nil {
  50. fmt.Println("配置重赋值失败,Err:", err)
  51. }
  52. fmt.Println(CONFIG)
  53. })
  54. if err := v.Unmarshal(&CONFIG); err != nil {
  55. fmt.Println("配置初始化赋值失败,Err:", err)
  56. }
  57. }