global.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package global
  2. import (
  3. "database/sql"
  4. "eta/eta_bridge/config"
  5. "eta/eta_bridge/utils"
  6. "fmt"
  7. "github.com/fsnotify/fsnotify"
  8. oplogging "github.com/op/go-logging"
  9. "github.com/spf13/viper"
  10. "gorm.io/gorm"
  11. "io"
  12. )
  13. var (
  14. CONFIG config.Config //配置文件
  15. LOG *oplogging.Logger
  16. FILE_LOG *oplogging.Logger // 自定义的输出日志
  17. MYSQL map[string]*gorm.DB //数据库连接配置
  18. MYSQL_LOG io.Writer
  19. DEFAULT_MYSQL *gorm.DB //默认数据库连接配置
  20. //Redis *redis.Client //redis链接
  21. OracleJy *sql.DB //嘉悦物产数据库连接
  22. OaDbHT *gorm.DB
  23. OaDbHTORM *sql.DB
  24. //Rc *cache.Cache //redis缓存
  25. Rc utils.RedisClient //redis缓存
  26. Re error //redis错误
  27. )
  28. const ConfigFile = "config_debug.yaml"
  29. const ProConfigFile = "config.yaml"
  30. const DefaultFilePath = "config"
  31. const ConfigFilePath = "/home/code/config/eta_bridge/config" //生产环境下的配置文件地址
  32. func init() {
  33. v := viper.New()
  34. configFilePath, _ := utils.ExistFiles(map[string][]string{
  35. DefaultFilePath: {
  36. ConfigFile,
  37. ProConfigFile,
  38. },
  39. ConfigFilePath: {
  40. ProConfigFile,
  41. },
  42. })
  43. //configFilePath := ConfigFile
  44. ////如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
  45. //if !utils.FileIsExist(configFilePath) {
  46. // configFilePath = ProConfigFile
  47. //}
  48. ////如果该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
  49. //if !utils.FileIsExist(configFilePath) {
  50. // configFilePath = ConfigFilePath
  51. //}
  52. fmt.Println("configFilePath->", configFilePath)
  53. //设置配置文件
  54. v.SetConfigFile(configFilePath)
  55. err := v.ReadInConfig()
  56. if err != nil {
  57. panic(fmt.Errorf("读取配置失败,Err: %s \n", err))
  58. }
  59. //持续监听文件
  60. v.WatchConfig()
  61. v.OnConfigChange(func(e fsnotify.Event) {
  62. fmt.Println("配置文件变更:", e.Name)
  63. handleConfig(v, "配置重赋值")
  64. })
  65. handleConfig(v, "配置初始化")
  66. return
  67. }
  68. // handleConfig
  69. // @Description: 配置文件处理
  70. // @author: Roc
  71. // @datetime 2024-03-13 10:27:40
  72. // @param v *viper.Viper
  73. // @param handleType string
  74. func handleConfig(v *viper.Viper, handleType string) {
  75. if err := v.Unmarshal(&CONFIG); err != nil {
  76. fmt.Println(handleType+"赋值失败,Err:", err)
  77. }
  78. if CONFIG.Mysql.Binlog.LogSaveDay == 0 {
  79. CONFIG.Mysql.Binlog.LogSaveDay = 31
  80. }
  81. //fmt.Println(CONFIG)
  82. //fmt.Println(CONFIG.Mysql.UpdateLogSaveDay)
  83. }