global.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. const DefaultFilePath = "config"
  20. const ConfigFilePath = "/home/code/config/eta_obs/config" //生产环境下的配置文件地址
  21. func init() {
  22. v := viper.New()
  23. configFilePath, _ := utils.ExistFiles(map[string][]string{
  24. DefaultFilePath: {
  25. ConfigFile,
  26. ProConfigFile,
  27. },
  28. ConfigFilePath: {
  29. ProConfigFile,
  30. },
  31. })
  32. ////如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
  33. //if !utils.FileIsExist(configFilePath) {
  34. // configFilePath = ProConfigFile
  35. //}
  36. fmt.Println("configFilePath->", configFilePath)
  37. //设置配置文件
  38. v.SetConfigFile(configFilePath)
  39. err := v.ReadInConfig()
  40. if err != nil {
  41. panic(fmt.Errorf("读取配置失败,Err: %s \n", err))
  42. }
  43. //持续监听文件
  44. v.WatchConfig()
  45. v.OnConfigChange(func(e fsnotify.Event) {
  46. fmt.Println("配置文件变更:", e.Name)
  47. handleConfig(v, "配置重赋值")
  48. })
  49. handleConfig(v, "配置初始化")
  50. return
  51. }
  52. // handleConfig
  53. // @Description: 配置文件处理
  54. // @author: Roc
  55. // @datetime 2024-03-13 10:27:40
  56. // @param v *viper.Viper
  57. // @param handleType string
  58. func handleConfig(v *viper.Viper, handleType string) {
  59. if err := v.Unmarshal(&CONFIG); err != nil {
  60. fmt.Println(handleType+"赋值失败,Err:", err)
  61. }
  62. //if CONFIG.Mysql.Binlog.LogSaveDay == 0 {
  63. // CONFIG.Mysql.Binlog.LogSaveDay = 31
  64. //}
  65. //fmt.Println(CONFIG)
  66. //fmt.Println(CONFIG.Mysql.UpdateLogSaveDay)
  67. }