config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package utils
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. beeLogger "github.com/beego/bee/v2/logger"
  8. "github.com/beego/beego/v2/server/web"
  9. "github.com/go-redis/redis/v8"
  10. )
  11. // 数据库配置
  12. var (
  13. RunMode string
  14. MYSQL_URL_MASTER string
  15. MYSQL_URL_RDDP string
  16. REDIS_CACHE string //缓存地址
  17. Redis *redis.Client //redis链接
  18. )
  19. // 日志配置
  20. var (
  21. LogPath string //调用过程中的日志存放地址
  22. LogFile string
  23. ApiLogPath string // 接口请求地址和接口返回值日志存放地址
  24. ApiLogFile string
  25. BinLogPath string // 数据库相关的日志存放地址
  26. BinLogFile string
  27. LogMaxDays int // 日志最大保留天数
  28. )
  29. var (
  30. SMS_TPLID string // 短信模板
  31. )
  32. var AlarmMsgUrl string // 报警地址
  33. // 微信相关
  34. var (
  35. WX_MINI_APPID string
  36. WX_MINI_APP_SECRET string
  37. //WX_ORIGIN_ID string
  38. //WX_APPID string
  39. //WX_APP_SECRET string
  40. )
  41. var SmsApiUrl string // 金瑞短信API调用地址
  42. var DesKey string // 接口返回加密KEY
  43. func init() {
  44. tmpRunMode, err := web.AppConfig.String("run_mode")
  45. if err != nil {
  46. panic(any("配置文件读取run_mode错误 " + err.Error()))
  47. }
  48. RunMode = tmpRunMode
  49. fmt.Println("RunMode:", RunMode)
  50. if RunMode == "" {
  51. configPath := `/home/code/config/eta_mini_api/conf/app.conf`
  52. fmt.Println("configPath:", configPath)
  53. err = web.LoadAppConfig("ini", configPath)
  54. if err != nil {
  55. fmt.Println("web.LoadAppConfig Err:" + err.Error())
  56. }
  57. tmpRunMode, _ := web.AppConfig.String("run_mode")
  58. RunMode = tmpRunMode
  59. }
  60. config, err := web.AppConfig.GetSection(RunMode)
  61. if err != nil {
  62. panic(any("配置文件读取错误 " + err.Error()))
  63. }
  64. beeLogger.Log.Info(RunMode + " 模式")
  65. MYSQL_URL_RDDP = config["mysql_url_rddp"]
  66. MYSQL_URL_MASTER = config["mysql_url_master"]
  67. SMS_TPLID = config["sms_tplId"]
  68. DesKey = config["des_key"]
  69. WX_MINI_APPID = config["wx_mini_appid"]
  70. WX_MINI_APP_SECRET = config["wx_mini_app_secret"]
  71. //WX_ORIGIN_ID = config["wx_origin_id"]
  72. //WX_APPID = config["wx_appid"]
  73. //WX_APP_SECRET = config["wx_app_secret"]
  74. SmsApiUrl = config["sms_api_url"]
  75. initRedis(config)
  76. }
  77. // initRedis 初始化redis配置
  78. func initRedis(config map[string]string) {
  79. REDIS_CACHE = config["beego_cache"]
  80. if len(REDIS_CACHE) <= 0 {
  81. panic("redis链接参数没有配置")
  82. }
  83. var redisConf map[string]string
  84. err := json.Unmarshal([]byte(REDIS_CACHE), &redisConf)
  85. if err != nil {
  86. panic("redis 配置异常失败:" + err.Error())
  87. }
  88. redisDb := 0 //默认使用redis的0库
  89. if dbStr, ok := redisConf["db"]; ok {
  90. redisDb, err = strconv.Atoi(dbStr)
  91. if err != nil {
  92. panic("redis 操作db库配置异常,db:" + dbStr)
  93. }
  94. }
  95. client := redis.NewClient(&redis.Options{
  96. Addr: redisConf["conn"],
  97. Password: redisConf["password"],
  98. DB: redisDb,
  99. //PoolSize: 10, //连接池最大socket连接数,默认为10倍CPU数, 10 * runtime.NumCPU(暂不配置)
  100. })
  101. _, err = client.Ping(context.TODO()).Result()
  102. if err != nil {
  103. panic("redis 链接失败:" + err.Error())
  104. }
  105. // 日志配置
  106. {
  107. LogPath = config["log_path"]
  108. LogFile = config["log_file"]
  109. BinLogPath = config["binlog_path"]
  110. BinLogFile = config["binlog_file"]
  111. ApiLogPath = config["apilog_path"]
  112. ApiLogFile = config["apilog_file"]
  113. logMaxDaysStr := config["log_max_day"]
  114. LogMaxDays, _ = strconv.Atoi(logMaxDaysStr)
  115. }
  116. //全局赋值redis链接
  117. Redis = client
  118. }