config.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package config
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.finogeeks.club/base/apm"
  6. "github.com/caarlos0/env/v6"
  7. )
  8. // 例子
  9. type Config struct {
  10. HttpPort string `env:"HTTP_PORT" envDefault:"8080"`
  11. GrpcPort string `env:"GRPC_PORT" envDefault:"9090"`
  12. ConsulAddr string `env:"" envDefault:""`
  13. ConsulHttpIp string `env:"CONSUL_HTTP_IP" envDefault:""`
  14. ConsulPort string `env:"CONSUL_PORT" envDefault:"8500"`
  15. ConsulTag string `env:"CONSUL_TAG" envDefault:"mop-finstore"` //consul tag
  16. Mode string `env:"MODE" envDefault:""`
  17. MongoURL string `env:"MONGO_URL" envDefault:"mongodb://localhost:27017"` //mongo地址
  18. DBName string `env:"DB_NAME" envDefault:"mop-account-system"`
  19. LogMode string `env:"LOG_MODE" envDefault:"debug"`
  20. HttpMode string `env:"HTTP_MODE" envDefault:"debug"`
  21. KongMode string `env:"KONG_MODE" envDefault:"use_db"`
  22. KongUrlPrefix string `env:"KONG_URL_PREFIX" envDefault:"http://gateway.gateway:8001"`
  23. CommonVerifyCode string `env:"COMMON_VERIFY_CODE" envDefault:"185890"`
  24. OrganExpiredM string `env:"ORGAN_EXPIRED_MONTH" envDefault:"12"` //企业过期时间 单位:month
  25. RedisAddr string `env:"REDIS_ADDR" envDefault:"redis://redis:6379/11"`
  26. RedisPassword string `env:"REDIS_PASSWORD" envDefault:""`
  27. RedisMode string `env:"REDIS_MODE" envDefault:"single"`
  28. ResetPassword string `env:"RESET_PASSWORD" envDefault:"123Abc"`
  29. OperAdminName string `env:"OPER_ADMIN_NAME" envDefault:"admin"`
  30. OperAdminAccount string `env:"OPER_ADMIN_ACCOUNT" envDefault:"admin"`
  31. OperAdminPhone string `env:"OPER_ADMIN_PHONE" envDefault:""`
  32. OperAdminPwd string `env:"OPER_ADMIN_PASSWORD" envDefault:"123Abc"`
  33. DeviceSecurityUrlPrefix string `env:"DEVICE_SECURITY_URL_PREFIX" envDefault:"http://mop-device-security:8080"`
  34. TokenExpireTime int `env:"TOKEN_EXPIRE_TIME" envDefault:"1036800"`
  35. MaxRedisClientNum int `env:"MAX_REDIS_CLIENT_NUM" envDefault:"100"`
  36. OperAdminEmail string `env:"OPER_ADMIN_EMAIL" envDefault:"admin@open.com"`
  37. KafkaAddr string `env:"KAFKA_ADDR" envDefault:"kafka-service.kafka:9093"`
  38. KafkaVersion string `env:"KAFKA_VERSION" envDefault:"2.3.0"`
  39. DevelopLevel string `env:"DEVELOP_LEVEL" envDefault:"release"`
  40. VerifyCodeUrlPrefix string `env:"VERIFY_CODE_SERVER_HOST" envDefault:"http://mop-verify-code-gateway:8080"`
  41. VerifyCodeServerHost string `env:"VERIFY_CODE_URL_PREFIX" envDefault:"http://mop-verify-code-server:8080"`
  42. SetLoginFirstServerHost string `env:"SET_LOGIN_FIRST_HOST" envDefault:"http://mop-fast-trial-manager:8080"`
  43. DelPhoneLimitUrl string `env:"DEL_PHONE_CODE_LIMIT_URL" envDefault:"/api/v1/mop/finogeeks-verify-code-server/phone-limit"`
  44. OperateLogTopic string `env:"OPERATE_LOG_TOPIC" envDefault:"mop_operation_log"`
  45. NotifyDataTopic string `env:"NOTIFY_DATA_TOPIC" envDefault:"mop_notify_data_topic"`
  46. CompanyUpdateTopic string `env:"COMPANY_UPDATE_TOPIC" envDefault:"mop_purchasing_rights_manager_topic"`
  47. ZipKinUrl string `env:"ZIPKIN_URL" envDefault:""`
  48. ServerName string `env:"SERVER_NAME" envDefault:"mop-account-system"`
  49. PwdErrCountLimit int `env:"PWD_ERR_COUNT_LIMITE" envDefault:"5"`
  50. PwdErrTimeLimit int `env:"PWD_ERR_TIME_LIMIT" envDefault:"86400"` //密码错误,账号封禁时间 单位:秒
  51. VerifyCodeType string `env:"VERIFY_CODE_TYPE" envDefault:"mop"`
  52. PerAccountExpireMon int `env:"PER_ACCOUNT_EXPIRE_MON" envDefault:"12"`
  53. NotifyFinstoreInfoUpUrl string `env:"FINSTORE_NOTIFY_INFO_URL" envDefault:"http://mop-app-manage-svr:8080/api/v1/mop/finstore/notify"`
  54. //数量限制
  55. AllowApplyIdNum int `env:"ALLOW_APPLY_ID_NUM" envDefault:"10"`
  56. AllowBindNum int `env:"ALLOW_BIND_NUM" envDefault:"10"`
  57. AllowMemNum int `env:"ALLOW_MEM_NUM" envDefault:"99"`
  58. PerAllowAppIdNum int `env:"PER_ALLOW_APPID_NUM" envDefault:"2"`
  59. PerAllowBindNum int `env:"PER_ALLOW_BIND_NUM" envDefault:"1"`
  60. PerAllowMemNum int `env:"PER_ALLOW_MEM_NUM" envDefault:"0"`
  61. Env string `env:"ENV" envDefault:"mop-uat"` //mop-uat、mop-fdep、mop-private、mop-community
  62. //redis
  63. RedisSentinelAddr string `env:"REDIS_SENTINEL_ADDR" envDefault:"redis://redis-cluster-redis-cluster-sentinel.redis-cluster:26379/"`
  64. RedisMasterName string `env:"REDIS_MASTER_NAME" envDefault:"mymaster"`
  65. RedisDatabase int `env:"REDIS_INDEX" envDefault:"11"`
  66. RedisSentinelPassword string `env:"REDIS_SENTINEL_PASSWORD" envDefault:""`
  67. RightUrlPrefix string `env:"RIGHT_UTL_PREFIX" envDefault:"http://mop-purchasing-rights-manager.mop-finstore:8080"`
  68. OrderRightsModifyURL string `env:"ORDER_RIGHTS_MODIFY_URL" envDefault:"http://mop-purchasing-rights-manager:8080/api/v1/mop/mop-purchasing-rights-manager/rights/modify"`
  69. MysqlUserName string `env:"MYSQL_USER_NAME" envDefault:"root"`
  70. MysqlPassword string `env:"MYSQL_PASSWORD" envDefault:""`
  71. MysqlURL string `env:"MYSQL_URL" envDefault:"localhost"`
  72. MysqlPort int `env:"MYSQL_PORT" envDefault:"3306"`
  73. DBMode string `env:"DB_MODE" envDefault:"mongo"`
  74. MysqlAddr string `env:"MYSQL_ADDR" envDefault:""`
  75. SkyWalkingUrl string `env:"SKYWALKING_URL" envDefault:"127.0.0.1:11800"`
  76. SkyWalkingEnable bool `env:"SKYWALKING_ENABLE" envDefault:"false"`
  77. SkyWalkingPartitions uint32 `env:"SKYWALKING_PARTITIONS" envDefault:"1"`
  78. AuditManageHost string `env:"AUDIT_MANAGE_HOST" envDefault:"http://mop-audit-manage-svr:8080"`
  79. }
  80. var Cfg *Config
  81. func init() {
  82. //Cfg = getConfig()
  83. Cfg = new(Config)
  84. if err := env.Parse(Cfg); err != nil {
  85. fmt.Printf("%+v\n", err)
  86. panic(err)
  87. }
  88. if Cfg.ConsulHttpIp != "" {
  89. Cfg.ConsulAddr = Cfg.ConsulHttpIp + ":" + Cfg.ConsulPort
  90. } else {
  91. Cfg.ConsulAddr = ""
  92. }
  93. b, _ := json.MarshalIndent(Cfg, "", " ")
  94. fmt.Println("config:", string(b))
  95. apm.BuildApmClient(apm.CreateBuild(Cfg.SkyWalkingUrl, Cfg.ServerName, Cfg.SkyWalkingPartitions, Cfg.SkyWalkingEnable))
  96. }
  97. func GetConfig() *Config {
  98. return Cfg
  99. }
  100. //func getConfig() (cfg *Config) {
  101. // config := &Config{}
  102. // config.HttpPort = getEnv("HTTP_PORT", "8080") //服务端口
  103. // config.GrpcPort = getEnv("GRPC_PORT", "9090") //grpc服务端口
  104. // config.ConsulHttpIp = getEnv("CONSUL_HTTP_IP", "")
  105. // config.ConsulPort = getEnv("CONSUL_PORT", "8500")
  106. // if config.ConsulHttpIp != "" {
  107. // config.ConsulAddr = config.ConsulHttpIp + ":" + config.ConsulPort
  108. // } else {
  109. // config.ConsulAddr = ""
  110. // }
  111. // config.ConsulTag = getEnv("CONSUL_TAG", "mop-finstore") //consul tag
  112. // config.Mode = getEnv("MODE", "") //路由模式
  113. // config.MongoURL = getEnv("MONGO_URL", "mongodb://localhost:27017") //mongo地址
  114. // config.DBName = getEnv("DB_NAME", "mop-applets-ecol-account") //db名字
  115. // config.LogMode = getEnv("LOG_MODE", "debug") //日志级别
  116. // config.HttpMode = getEnv("HTTP_MODE", "debug") //http的mode 分"debug" "test" "release"
  117. // config.KongUrlPrefix = getEnv("KONG_URL_PREFIX", "http://gateway.gateway:8001")
  118. // config.OrganExpiredM = getEnv("ORGAN_EXPIRED_MONTH", "12") //企业过期时间
  119. // config.CommonVerifyCode = getEnv("COMMON_VERIFY_CODE", "185890") //万能验证码,为空的时候不能使用该验证码
  120. // config.RedisAddr = getEnv("REDIS_URL", "redis://redis:6379/11")
  121. // config.RedisPassword = getEnv("REDIS_PASSWORD", "")
  122. // config.RedisMode = getEnv("REDIS_MODE", "single") //singel:单点模式,sentinel:哨兵模式
  123. // config.ResetPassword = getEnv("RESET_PASSWORD", "123Abc") //重置成员密码
  124. // config.OperAdminName = getEnv("OPER_ADMIN_NAME", "admin") //运营方管理员姓名
  125. // config.OperAdminAccount = getEnv("OPER_ADMIN_ACCOUNT", "admin") //运营方管理员账号
  126. // config.OperAdminPhone = getEnv("OPER_ADMIN_PHONE", "") //运营方管理员电话
  127. // config.OperAdminPwd = getEnv("OPER_ADMIN_PASSWORD", "123Abc") //运营方管理员密码
  128. // config.DeviceSecurityUrlPrefix = getEnv("DEVICE_SECURITY_URL_PREFIX", "http://mop-device-security:8080")
  129. // config.TokenExpireTime = getEnvNum("TOKEN_EXPIRE_TIME", 86400*14)
  130. // config.MaxRedisClientNum = getEnvNum("MAX_REDIS_CLIENT_NUM", 100)
  131. // config.OperAdminEmail = getEnv("OPER_ADMIN_EMAIL", "admin@open.com")
  132. // config.KafkaAddr = getEnv("KAFKA_ADDR", "kafka-service.kafka:9093")
  133. // config.DevelopLevel = getEnv("DEVELOP_LEVEL", "release")
  134. // config.VerifyCodeServerHost = getEnv("VERIFY_CODE_SERVER_HOST", "http://mop-verify-code-server:8080")
  135. // config.VerifyCodeUrlPrefix = getEnv("VERIFY_CODE_URL_PREFIX", "http://mop-verify-code-gateway:8080")
  136. // config.DelPhoneLimitUrl = getEnv("DEL_PHONE_CODE_LIMIT_URL", "/api/v1/mop/finogeeks-verify-code-server/phone-limit")
  137. // config.OperateLogTopic = getEnv("OPERATE_LOG_TOPIC", "mop_operation_log")
  138. // config.NotifyDataTopic = getEnv("NOTIFY_DATA_TOPIC", "mop_notify_data_topic")
  139. // config.ZipKinUrl = getEnv("ZIPKIN_URL", "")
  140. // config.ServerName = getEnv("SERVER_NAME", "mop-account-system")
  141. // config.PwdErrCountLimit = getEnvNum("PWD_ERR_COUNT_LIMITE", 5)
  142. // config.PwdErrTimeLimit = getEnvNum("PWD_ERR_TIME_LIMIT", 24*60*60)
  143. // config.VerifyCodeType = getEnv("VERIFY_CODE_TYPE", "mop")
  144. // config.PerAccountExpireMon = getEnvNum("PER_ACCOUNT_EXPIRE_MON", 12)
  145. //
  146. // config.NotifyFinstoreInfoUpUrl = getEnv("FINSTORE_NOTIFY_INFO_URL", "http://mop-app-manage-svr:8080/api/v1/mop/finstore/notify")
  147. // //私有化版本以license为准、saas版以环境变量为准
  148. // config.AllowApplyIdNum = getEnvNum("ALLOW_APPLY_ID_NUM", 10) //初始允许企业创建的最大appid数量
  149. // config.AllowBindNum = getEnvNum("ALLOW_BIND_NUM", 10) //初始允许企业创建的最大应用数量
  150. // config.AllowMemNum = getEnvNum("ALLOW_MEM_NUM", 99) //初始允许企业创建的最大成员数量
  151. // config.PerAllowAppIdNum = getEnvNum("PER_ALLOW_APPID_NUM", 2) //初始个人mock企业创建的最大appid数量
  152. // config.PerAllowBindNum = getEnvNum("PER_ALLOW_BIND_NUM", 1) //初始个人mock企业创建的最大应用数量
  153. // config.PerAllowMemNum = getEnvNum("PER_ALLOW_MEM_NUM", 0) //初始个人mocke企业申请的最大成员数量
  154. //
  155. // config.RedisSentinelAddr = getEnv("REDIS_SENTINEL_ADDR", "redis://redis-cluster-redis-cluster-sentinel.redis-cluster:26379/")
  156. // config.RedisMasterName = getEnv("REDIS_MASTER_NAME", "mymaster")
  157. // config.RedisDatabase = getEnvNum("REDIS_INDEX", 11)
  158. // config.RedisSentinelPassword = getEnv("REDIS_SENTINEL_PASSWORD", "")
  159. //
  160. // config.Env = getEnv("ENV", "mop-uat") //mop-uat、mop-fdep、mop-private
  161. // config.RightUrlPrefix = getEnv("RIGHT_UTL_PREFIX", "http://mop-purchasing-rights-manager.mop-finstore:8080")
  162. // fmt.Printf("config:%+v\n", config)
  163. // return config
  164. //}
  165. //
  166. //func getEnv(key, fallback string) string {
  167. // if value, ok := os.LookupEnv(key); ok {
  168. // return value
  169. // }
  170. // return fallback
  171. //}
  172. //
  173. //func getEnvNum(key string, fallback int) int {
  174. // if value, ok := os.LookupEnv(key); ok {
  175. // num, err := strconv.Atoi(value)
  176. // if err != nil {
  177. // panic("env param is not int")
  178. // }
  179. // return num
  180. // }
  181. // return fallback
  182. //}