config.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package utils
  2. import (
  3. "fmt"
  4. beeLogger "github.com/beego/bee/v2/logger"
  5. "github.com/beego/beego/v2/server/web"
  6. "go.mongodb.org/mongo-driver/mongo"
  7. "strconv"
  8. )
  9. var (
  10. RunMode string //运行模式
  11. APP_NAME_CN string
  12. MYSQL_URL string //数据库连接
  13. /*MONGODB_URL string //mongodb连接
  14. MONGODB_COMMUNITY string //mongodb库名
  15. MONGODB_CREDENTIAL string*/
  16. Re error //redis错误
  17. Rc RedisClient //redis缓存
  18. MgoUrlData string // mongodb数据库连接配置
  19. MgoDataCli *mongo.Client // mongodb客户端连接
  20. MgoDataDbName string // mongodb指标数据的库名
  21. )
  22. var (
  23. // APP_NAME_EN 项目英文名称
  24. APP_NAME_EN string
  25. // Md5Key 签名秘钥
  26. Md5Key string
  27. )
  28. // ES配置
  29. var (
  30. ES_URL string // ES服务器地址
  31. ES_USERNAME string // ES账号
  32. ES_PASSWORD string // ES密码
  33. )
  34. // ES索引配置
  35. var (
  36. DATA_INDEX_NAME string //数据指标库索引
  37. CHART_INDEX_NAME string //研究图库索引
  38. )
  39. // 弘则
  40. const (
  41. APPID = "SJCrpOPvagscPxEv"
  42. SECRET = "gLLjT72uFHQZEFtaFCuoZegD1z2ezfyX"
  43. )
  44. // 日志配置
  45. var (
  46. LogPath string //调用过程中的日志存放地址
  47. LogFile string
  48. LogDataPath string //调用过程中图表相关的日志存放地址
  49. LogDataFile string
  50. BinLogPath string //数据库相关的日志存放地址
  51. BinLogFile string
  52. ApiLogPath string //接口请求地址和接口返回值日志存放地址
  53. ApiLogFile string
  54. LogMaxDays int //日志最大保留天数
  55. )
  56. // AlarmMsgUrl 报警服务地址
  57. var AlarmMsgUrl string
  58. func init() {
  59. tmpRunMode, err := web.AppConfig.String("run_mode")
  60. if err != nil {
  61. panic("配置文件读取run_mode错误 " + err.Error())
  62. }
  63. RunMode = tmpRunMode
  64. fmt.Println("RunMode:", RunMode)
  65. if RunMode == "" {
  66. localIp, err := GetLocalIP()
  67. fmt.Println("localIp:", localIp)
  68. if localIp == "10.0.0.123" {
  69. RunMode = "debug"
  70. } else {
  71. RunMode = "release"
  72. }
  73. fmt.Println("RunMode:", RunMode)
  74. configPath := `/home/code/config/eta_forum_hub/conf/app.conf`
  75. fmt.Println("configPath:", configPath)
  76. err = web.LoadAppConfig("ini", configPath)
  77. if err != nil {
  78. fmt.Println("web.LoadAppConfig Err:" + err.Error())
  79. }
  80. }
  81. config, err := web.AppConfig.GetSection(RunMode)
  82. if err != nil {
  83. panic("配置文件读取错误 " + err.Error())
  84. }
  85. beeLogger.Log.Info(RunMode + " 模式")
  86. // 项目中文名称
  87. appNameCn, err := web.AppConfig.String("app_name_cn")
  88. if err != nil {
  89. panic(any("配置文件读取app_name_cn错误 " + err.Error()))
  90. }
  91. APP_NAME_CN = appNameCn
  92. // 项目英文文名称
  93. APP_NAME_EN = config["app_name_en"]
  94. // 签名秘钥
  95. Md5Key = config["md5_key"]
  96. MYSQL_URL = config["mysql_url"]
  97. // mongodb数据库连接配置
  98. MgoUrlData = config["mgo_url_data"]
  99. //日志配置
  100. {
  101. LogPath = config["log_path"]
  102. LogFile = config["log_file"]
  103. LogDataPath = config["log_data_path"]
  104. LogDataFile = config["log_data_file"]
  105. BinLogPath = config["binlog_path"]
  106. BinLogFile = config["binlog_file"]
  107. ApiLogPath = config["apilog_path"]
  108. ApiLogFile = config["apilog_file"]
  109. logMaxDaysStr := config["log_max_day"]
  110. LogMaxDays, _ = strconv.Atoi(logMaxDaysStr)
  111. }
  112. {
  113. // 报警服务地址
  114. AlarmMsgUrl = config["alarm_msg_url"]
  115. }
  116. // 初始化缓存
  117. redisClient, err := initRedis(config["redis_type"], config["beego_cache"])
  118. if err != nil {
  119. fmt.Println("redis链接异常:", err)
  120. panic(any(Re))
  121. }
  122. Rc = redisClient
  123. // ES配置
  124. {
  125. ES_URL = config["es_url"]
  126. ES_USERNAME = config["es_username"]
  127. ES_PASSWORD = config["es_password"]
  128. }
  129. // 初始化ES
  130. initEs()
  131. // ES 索引
  132. {
  133. DATA_INDEX_NAME = config["data_index_name"]
  134. CHART_INDEX_NAME = config["chart_index_name"]
  135. }
  136. }
  137. //修改接口文档
  138. //http://8.136.199.33:8300/swagger/