global.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package global
  2. import (
  3. "database/sql"
  4. "eta_gn/eta_bridge/config"
  5. "eta_gn/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. OracleJy *sql.DB //嘉悦物产数据库连接
  21. Rc utils.RedisClient //redis缓存
  22. Re error //redis错误
  23. )
  24. const ConfigFile = "config/config_debug.yaml" //本地(测试)环境下的配置文件地址
  25. const ProConfigFile = "config/config.yaml" //生产环境下的配置文件地址
  26. func init() {
  27. v := viper.New()
  28. configFilePath := ConfigFile
  29. if !utils.FileIsExist(configFilePath) {
  30. configFilePath = ProConfigFile
  31. }
  32. fmt.Println("configFilePath->", configFilePath)
  33. v.SetConfigFile(configFilePath)
  34. err := v.ReadInConfig()
  35. if err != nil {
  36. panic(fmt.Errorf("读取配置失败,Err: %s \n", err))
  37. }
  38. v.WatchConfig()
  39. v.OnConfigChange(func(e fsnotify.Event) {
  40. fmt.Println("配置文件变更:", e.Name)
  41. handleConfig(v, "配置重赋值")
  42. })
  43. handleConfig(v, "配置初始化")
  44. return
  45. }
  46. func handleConfig(v *viper.Viper, handleType string) {
  47. if err := v.Unmarshal(&CONFIG); err != nil {
  48. fmt.Println(handleType+"赋值失败,Err:", err)
  49. }
  50. if CONFIG.Mysql.Binlog.LogSaveDay == 0 {
  51. CONFIG.Mysql.Binlog.LogSaveDay = 31
  52. }
  53. }