global.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package global
  2. import (
  3. "eta_gn/eta_obs/config"
  4. "eta_gn/eta_obs/utils"
  5. "fmt"
  6. "github.com/fsnotify/fsnotify"
  7. "github.com/minio/minio-go/v7"
  8. oplogging "github.com/op/go-logging"
  9. "github.com/spf13/viper"
  10. )
  11. var (
  12. CONFIG config.Config //配置文件
  13. LOG *oplogging.Logger
  14. FILE_LOG *oplogging.Logger // 自定义的输出日志
  15. MinioClient *minio.Client
  16. )
  17. const ConfigFile = "config/config_debug.yaml" //本地(测试)环境下的配置文件地址
  18. const ProConfigFile = "config/config.yaml" //生产环境下的配置文件地址
  19. func init() {
  20. v := viper.New()
  21. configFilePath := ConfigFile
  22. //如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
  23. if !utils.FileIsExist(configFilePath) {
  24. configFilePath = ProConfigFile
  25. }
  26. fmt.Println("configFilePath->", configFilePath)
  27. //设置配置文件
  28. v.SetConfigFile(configFilePath)
  29. err := v.ReadInConfig()
  30. if err != nil {
  31. panic(fmt.Errorf("读取配置失败,Err: %s \n", err))
  32. }
  33. //持续监听文件
  34. v.WatchConfig()
  35. v.OnConfigChange(func(e fsnotify.Event) {
  36. fmt.Println("配置文件变更:", e.Name)
  37. handleConfig(v, "配置重赋值")
  38. })
  39. handleConfig(v, "配置初始化")
  40. return
  41. }
  42. // handleConfig
  43. // @Description: 配置文件处理
  44. // @author: Roc
  45. // @datetime 2024-03-13 10:27:40
  46. // @param v *viper.Viper
  47. // @param handleType string
  48. func handleConfig(v *viper.Viper, handleType string) {
  49. if err := v.Unmarshal(&CONFIG); err != nil {
  50. fmt.Println(handleType+"赋值失败,Err:", err)
  51. }
  52. //if CONFIG.Mysql.Binlog.LogSaveDay == 0 {
  53. // CONFIG.Mysql.Binlog.LogSaveDay = 31
  54. //}
  55. //fmt.Println(CONFIG)
  56. //fmt.Println(CONFIG.Mysql.UpdateLogSaveDay)
  57. }