app.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package config
  2. import (
  3. "crypto/rsa"
  4. "crypto/tls"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "sync"
  9. "time"
  10. "gitee.com/xuyiping_admin/pkg/di"
  11. )
  12. var (
  13. Module = di.Provide(Options)
  14. options *AppConfig
  15. appEnv string
  16. initOnce sync.Once
  17. )
  18. // AppConfig store all configuration options
  19. type AppConfig struct {
  20. FarmName string `json:"farm_name"`
  21. AppName string `yaml:"app_name"`
  22. AppEnv string `yaml:"app_environment"`
  23. Debug bool `yaml:"debug" env:"APP_DEBUG"`
  24. HTTPServerAddr string `yaml:"http_server_addr" env:"HTTP_SERVER_ADDR"`
  25. // 数据库配置 额外加载文件部分 database.yaml
  26. StoreSetting StoreSetting `json:"storeSetting" yaml:"store"`
  27. // redis 配置
  28. RedisSetting RedisSetting `json:"RedisSetting" yaml:"redis_setting"`
  29. JwtSecret string `json:"jwtSecret" yaml:"jwt_secret"`
  30. ExcelSetting ExcelSetting `json:"excelSetting" yaml:"excel_setting"`
  31. WechatSetting WechatSetting `json:"wechatSetting" yaml:"wechat_setting"`
  32. JwtTokenKeyConfig JwtTokenKeyConfig `json:"jwtTokenKeyConfig"`
  33. JwtExpireTime int `json:"jwtExpireTime" yaml:"jwt_expire_time"`
  34. // asynq 相关配置
  35. SideWorkSetting SideWorkSetting `yaml:"side_work_setting"`
  36. }
  37. type JwtTokenKeyConfig struct {
  38. PrivateKey *rsa.PrivateKey `json:"privateKey"`
  39. PublicKey *rsa.PublicKey `json:"publicKey"`
  40. }
  41. type WechatSetting struct {
  42. Appid string `yaml:"appid"`
  43. Secret string `yaml:"secret"`
  44. }
  45. type ExcelSetting struct {
  46. SheetName string `yaml:"sheet_name"` // = "Sheet1" //默认Sheet名称
  47. Height float64 `yaml:"height"` // = 25.0 //默认行高度
  48. }
  49. // StoreSetting 数据库配置
  50. type StoreSetting struct {
  51. // 开启 SyDb SQL 记录
  52. DriverName string `yaml:"driver_name" json:"driver_name"`
  53. ShowSQL bool `yaml:"show_sql" json:"show_sql"`
  54. KptRW string `yaml:"kpt_rw" json:"kpt_rw"`
  55. KptMigr string `yaml:"kpt_migr" json:"kpt_migr"`
  56. }
  57. type RedisSetting struct {
  58. // sso 配置
  59. CacheRedis CacheRedisDB `json:"cache_redis" yaml:"cache_redis"`
  60. }
  61. type CacheRedisDB struct {
  62. Addr string `json:"addr" yaml:"addr"`
  63. DB int `json:"db" yaml:"db"`
  64. Requirepass string `json:"requirepass" yaml:"requirepass"`
  65. Expiry int `json:"expiry" yaml:"expiry"`
  66. }
  67. type SideWorkSetting struct {
  68. // Asynq 配置
  69. AsynqSetting AsynqSetting `json:"asynq_setting,omitempty" yaml:"asynq_setting"`
  70. }
  71. type AsynqSetting struct {
  72. Redis AsynqRedisSetting `json:"redis" yaml:"redis"`
  73. Queues map[string]int `json:"queues,omitempty" yaml:"queues"`
  74. Concurrency int `json:"concurrency,omitempty" yaml:"concurrency"`
  75. LogLevel int32 `json:"log_level,omitempty" yaml:"log_level"`
  76. }
  77. type AsynqRedisSetting struct {
  78. // Network type to use, either tcp or unix.
  79. // Default is tcp.
  80. Network string `json:"network,omitempty" yaml:"network"`
  81. // Redis server address in "host:port" format.
  82. Addr string `json:"addr,omitempty" yaml:"addr"`
  83. // Username to authenticate the current connection when Redis ACLs are used.
  84. // See: https://redis.io/commands/auth.
  85. Username string `json:"username,omitempty" yaml:"username"`
  86. // Password to authenticate the current connection.
  87. // See: https://redis.io/commands/auth.
  88. Password string `json:"password,omitempty" yaml:"password"`
  89. // Redis DB to select after connecting to a server.
  90. // See: https://redis.io/commands/select.
  91. DB int `json:"db,omitempty" yaml:"db"`
  92. // Dial timeout for establishing new connections.
  93. // Default is 5 seconds.
  94. DialTimeout time.Duration `json:"dialTimeout,omitempty" yaml:"dial_timeout"`
  95. // Timeout for socket reads.
  96. // If timeout is reached, read commands will fail with a timeout error
  97. // instead of blocking.
  98. //
  99. // Use value -1 for no timeout and 0 for default.
  100. // Default is 3 seconds.
  101. ReadTimeout time.Duration `json:"readTimeout,omitempty" yaml:"read_timeout"`
  102. // Timeout for socket writes.
  103. // If timeout is reached, write commands will fail with a timeout error
  104. // instead of blocking.
  105. //
  106. // Use value -1 for no timeout and 0 for default.
  107. // Default is ReadTimout.
  108. WriteTimeout time.Duration `json:"writeTimeout,omitempty" yaml:"write_timeout"`
  109. // Maximum number of socket connections.
  110. // Default is 10 connections per every CPU as reported by runtime.NumCPU.
  111. PoolSize int `json:"poolSize,omitempty" yaml:"pool_size"`
  112. // TLS Config used to connect to a server.
  113. // TLS will be negotiated only if this field is set.
  114. TLSConfig *tls.Config `json:"tlsConfig,omitempty" yaml:"tls_config"`
  115. }
  116. func (a *AppConfig) Name() string {
  117. return fmt.Sprintf("%s-%s", a.AppName, a.AppEnv)
  118. }
  119. // CacheNameSpace 作为 Key 的前缀,用来区分不同环境,不同 APP 下的 Key,防止缓存干扰
  120. // 踩坑记录:
  121. //
  122. // 如果使用 fmt.Sprintf("%s-%s-%s", a.AppName, a.AppRole, a.AppEnv),例如 sayam-http-production, sayam-consumer-production
  123. // 会导致 从 role http 写入的 key,从 role consumer 中取不出来,反之亦然
  124. // 支持更多的key空间, 可以更灵活的定义ns
  125. func (a *AppConfig) CacheNameSpace() string {
  126. cacheKeySpace := ""
  127. if a.FarmName != "" {
  128. cacheKeySpace = fmt.Sprintf("%s-%s-%s", a.AppName, a.AppEnv, a.FarmName)
  129. } else {
  130. cacheKeySpace = fmt.Sprintf("%s-%s", a.AppName, a.AppEnv)
  131. }
  132. return cacheKeySpace
  133. }
  134. func Options() *AppConfig {
  135. return options
  136. }
  137. func init() {
  138. appEnv = strings.ToLower(os.Getenv("APP_ENVIRONMENT"))
  139. cfg := &AppConfig{}
  140. var err error
  141. initOnce.Do(func() {
  142. switch appEnv {
  143. case "test":
  144. err = Initialize("app.test.yaml", cfg)
  145. case "development":
  146. err = Initialize("app.develop.yaml", cfg)
  147. case "production":
  148. err = Initialize("app.production.yaml", cfg)
  149. default:
  150. panic("err confing")
  151. }
  152. if err != nil {
  153. panic(err)
  154. }
  155. cfg.JwtTokenKeyConfig = openPrivateKey()
  156. options = cfg
  157. })
  158. }