config.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package utils
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/beego/beego/v2/core/logs"
  7. "github.com/beego/beego/v2/server/web"
  8. "github.com/go-redis/redis/v8"
  9. "strconv"
  10. )
  11. var (
  12. RunMode string //运行模式
  13. MYSQL_URL string //数据库连接
  14. MYSQL_URL_RDDP string //数据库连接
  15. MYSQL_URL_EDB string
  16. REDIS_CACHE string //缓存地址
  17. Redis *redis.Client //redis链接
  18. )
  19. var (
  20. STATIC_DIR string
  21. )
  22. //微信配置信息
  23. var (
  24. WxId string //微信原始ID
  25. WxAppId string
  26. WxAppSecret string
  27. TemplateIdByProduct string //产品运行报告通知-模板ID
  28. TemplateRedirectUrl string //模板消息跳转地址
  29. WxPlatform int //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
  30. )
  31. //pc端微信配置信息
  32. var (
  33. PcWxId string //微信原始ID
  34. PcWxAppId string
  35. PcWxAppSecret string
  36. PcTemplateIdByProduct string //产品运行报告通知-模板ID
  37. PcTemplateRedirectUrl string //模板消息跳转地址
  38. WxPcPlatform int //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:3
  39. )
  40. func init() {
  41. tmpRunMode, err := web.AppConfig.String("run_mode")
  42. if err != nil {
  43. panic("配置文件读取run_mode错误 " + err.Error())
  44. }
  45. RunMode = tmpRunMode
  46. if RunMode == "" {
  47. localIp, err := GetLocalIP()
  48. fmt.Println("localIp:", localIp)
  49. if localIp == "10.0.0.123" {
  50. RunMode = "debug"
  51. } else {
  52. RunMode = "release"
  53. }
  54. fmt.Println("RunMode:", RunMode)
  55. configPath := `/home/code/config/hongze_api/conf/app.conf`
  56. fmt.Println("configPath:", configPath)
  57. err = web.LoadAppConfig("ini", configPath)
  58. if err != nil {
  59. fmt.Println("web.LoadAppConfig Err:" + err.Error())
  60. }
  61. //RunMode = "release"
  62. //configPath := `/home/code/config/hongze_api/conf/app.conf`
  63. //err := web.LoadAppConfig("ini", configPath)
  64. //if err != nil {
  65. // fmt.Println("web.LoadAppConfig Err:" + err.Error())
  66. //}
  67. }
  68. config, err := web.AppConfig.GetSection(RunMode)
  69. if err != nil {
  70. panic("配置文件读取错误 " + err.Error())
  71. }
  72. logs.Info(RunMode + " 模式")
  73. MYSQL_URL = config["mysql_url"]
  74. MYSQL_URL_RDDP = config["mysql_url_rddp"]
  75. MYSQL_URL_EDB = config["mysql_url_edb"]
  76. if RunMode == "release" {
  77. WxAppId = "wx4a844c734d8c8e56"
  78. WxAppSecret = "26c586e7ccb3c575433f0f37797b3eeb"
  79. WxId = "gh_b67e0049fb8c"
  80. TemplateIdByProduct = "Cp2wF8gvBtxyWV4DeYuI172oqwyYXVRSm3AyJO42d84"
  81. TemplateRedirectUrl = "https://ficc.hzinsights.com/reportdtl?id="
  82. WxPlatform = 1
  83. WxPcPlatform = 3
  84. PcWxAppId = "wx615472d6874eeb7f"
  85. PcWxAppSecret = "97fe374fb0cc90ef58c4b49d431366f1"
  86. STATIC_DIR = "/home/static/imgs/"
  87. } else {
  88. WxAppId = "wx9b5d7291e581233a"
  89. WxAppSecret = "f4d52e34021eee262dce9682b31f8861"
  90. WxId = "gh_5dc508325c6f"
  91. TemplateIdByProduct = "-YjuPOB7Fqd-S3ilabYa6wvjDY9aXmeEfPN6DCiy-EY"
  92. TemplateRedirectUrl = "http://rddpweb.brilliantstart.cn/reportdtl?id="
  93. WxPlatform = 1
  94. WxPcPlatform = 3
  95. PcWxAppId = "wx7c8084f6e5b1d85a"
  96. PcWxAppSecret = "9e4210cd5a363aa1f316b7c4b8898418"
  97. STATIC_DIR = "/home/static/imgs/"
  98. }
  99. // 初始化redis缓存
  100. initRedis(config)
  101. }
  102. // initRedis 初始化redis配置
  103. func initRedis(config map[string]string) {
  104. REDIS_CACHE = config["beego_cache"]
  105. if len(REDIS_CACHE) <= 0 {
  106. panic("redis链接参数没有配置")
  107. }
  108. var redisConf map[string]string
  109. err := json.Unmarshal([]byte(REDIS_CACHE), &redisConf)
  110. if err != nil {
  111. panic("redis 配置异常失败:" + err.Error())
  112. }
  113. redisDb := 0 //默认使用redis的0库
  114. if dbStr, ok := redisConf["db"]; ok {
  115. redisDb, err = strconv.Atoi(dbStr)
  116. if err != nil {
  117. panic("redis 操作db库配置异常,db:" + dbStr)
  118. }
  119. }
  120. client := redis.NewClient(&redis.Options{
  121. Addr: redisConf["conn"],
  122. Password: redisConf["password"],
  123. DB: redisDb,
  124. //PoolSize: 10, //连接池最大socket连接数,默认为10倍CPU数, 10 * runtime.NumCPU(暂不配置)
  125. })
  126. _, err = client.Ping(context.TODO()).Result()
  127. if err != nil {
  128. panic("redis 链接失败:" + err.Error())
  129. }
  130. //全局赋值redis链接
  131. Redis = client
  132. }
  133. //http://webapi.brilliantstart.cn/api/
  134. //http://webapi.brilliantstart.cn/swagger/
  135. //http://139.196.122.219:8603/swagger/