12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package global
- import (
- "database/sql"
- "eta_gn/eta_bridge/config"
- "eta_gn/eta_bridge/utils"
- "fmt"
- "github.com/fsnotify/fsnotify"
- oplogging "github.com/op/go-logging"
- "github.com/spf13/viper"
- "gorm.io/gorm"
- "io"
- )
- var (
- CONFIG config.Config //配置文件
- LOG *oplogging.Logger
- FILE_LOG *oplogging.Logger // 自定义的输出日志
- MYSQL map[string]*gorm.DB //数据库连接配置
- MYSQL_LOG io.Writer
- DEFAULT_MYSQL *gorm.DB //默认数据库连接配置
- OracleJy *sql.DB //嘉悦物产数据库连接
- Rc utils.RedisClient //redis缓存
- Re error //redis错误
- )
- const ConfigFile = "config/config_debug.yaml" //本地(测试)环境下的配置文件地址
- const ProConfigFile = "config/config.yaml" //生产环境下的配置文件地址
- func init() {
- v := viper.New()
- configFilePath := ConfigFile
- if !utils.FileIsExist(configFilePath) {
- configFilePath = ProConfigFile
- }
- fmt.Println("configFilePath->", configFilePath)
- v.SetConfigFile(configFilePath)
- err := v.ReadInConfig()
- if err != nil {
- panic(fmt.Errorf("读取配置失败,Err: %s \n", err))
- }
- v.WatchConfig()
- v.OnConfigChange(func(e fsnotify.Event) {
- fmt.Println("配置文件变更:", e.Name)
- handleConfig(v, "配置重赋值")
- })
- handleConfig(v, "配置初始化")
- return
- }
- func handleConfig(v *viper.Viper, handleType string) {
- if err := v.Unmarshal(&CONFIG); err != nil {
- fmt.Println(handleType+"赋值失败,Err:", err)
- }
- if CONFIG.Mysql.Binlog.LogSaveDay == 0 {
- CONFIG.Mysql.Binlog.LogSaveDay = 31
- }
- }
|