app.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. CronSetting CronSetting `json:"cron_setting" yaml:"cron"`
  37. }
  38. type CronSetting struct {
  39. // 是否启动任务时先跑一次
  40. CrontabStartRun bool `yaml:"crontab_start_run"`
  41. // CRONTAB 表达式
  42. UpdateCowDayAge string `yaml:"update_cow_dayAge"`
  43. GenerateWorkOrder string `yaml:"generate_work_order"`
  44. }
  45. type JwtTokenKeyConfig struct {
  46. PrivateKey *rsa.PrivateKey `json:"privateKey"`
  47. PublicKey *rsa.PublicKey `json:"publicKey"`
  48. }
  49. type WechatSetting struct {
  50. Appid string `yaml:"appid"`
  51. Secret string `yaml:"secret"`
  52. }
  53. type ExcelSetting struct {
  54. SheetName string `yaml:"sheet_name"` // = "Sheet1" //默认Sheet名称
  55. Height float64 `yaml:"height"` // = 25.0 //默认行高度
  56. }
  57. // StoreSetting 数据库配置
  58. type StoreSetting struct {
  59. // 开启 SyDb SQL 记录
  60. DriverName string `yaml:"driver_name" json:"driver_name"`
  61. ShowSQL bool `yaml:"show_sql" json:"show_sql"`
  62. KptRW string `yaml:"kpt_rw" json:"kpt_rw"`
  63. KptMigr string `yaml:"kpt_migr" json:"kpt_migr"`
  64. }
  65. type RedisSetting struct {
  66. // sso 配置
  67. CacheRedis CacheRedisDB `json:"cache_redis" yaml:"cache_redis"`
  68. }
  69. type CacheRedisDB struct {
  70. Addr string `json:"addr" yaml:"addr"`
  71. DB int `json:"db" yaml:"db"`
  72. Requirepass string `json:"requirepass" yaml:"requirepass"`
  73. Expiry int `json:"expiry" yaml:"expiry"`
  74. }
  75. type SideWorkSetting struct {
  76. // Asynq 配置
  77. AsynqSetting AsynqSetting `json:"asynq_setting,omitempty" yaml:"asynq_setting"`
  78. }
  79. type AsynqSetting struct {
  80. Redis AsynqRedisSetting `json:"redis" yaml:"redis"`
  81. Queues map[string]int `json:"queues,omitempty" yaml:"queues"`
  82. Concurrency int `json:"concurrency,omitempty" yaml:"concurrency"`
  83. LogLevel int32 `json:"log_level,omitempty" yaml:"log_level"`
  84. }
  85. type AsynqRedisSetting struct {
  86. // Network type to use, either tcp or unix.
  87. // Default is tcp.
  88. Network string `json:"network,omitempty" yaml:"network"`
  89. // Redis server address in "host:port" format.
  90. Addr string `json:"addr,omitempty" yaml:"addr"`
  91. // Username to authenticate the current connection when Redis ACLs are used.
  92. // See: https://redis.io/commands/auth.
  93. Username string `json:"username,omitempty" yaml:"username"`
  94. // Password to authenticate the current connection.
  95. // See: https://redis.io/commands/auth.
  96. Password string `json:"password,omitempty" yaml:"password"`
  97. // Redis DB to select after connecting to a server.
  98. // See: https://redis.io/commands/select.
  99. DB int `json:"db,omitempty" yaml:"db"`
  100. // Dial timeout for establishing new connections.
  101. // Default is 5 seconds.
  102. DialTimeout time.Duration `json:"dialTimeout,omitempty" yaml:"dial_timeout"`
  103. // Timeout for socket reads.
  104. // If timeout is reached, read commands will fail with a timeout error
  105. // instead of blocking.
  106. //
  107. // Use value -1 for no timeout and 0 for default.
  108. // Default is 3 seconds.
  109. ReadTimeout time.Duration `json:"readTimeout,omitempty" yaml:"read_timeout"`
  110. // Timeout for socket writes.
  111. // If timeout is reached, write commands will fail with a timeout error
  112. // instead of blocking.
  113. //
  114. // Use value -1 for no timeout and 0 for default.
  115. // Default is ReadTimout.
  116. WriteTimeout time.Duration `json:"writeTimeout,omitempty" yaml:"write_timeout"`
  117. // Maximum number of socket connections.
  118. // Default is 10 connections per every CPU as reported by runtime.NumCPU.
  119. PoolSize int `json:"poolSize,omitempty" yaml:"pool_size"`
  120. // TLS Config used to connect to a server.
  121. // TLS will be negotiated only if this field is set.
  122. TLSConfig *tls.Config `json:"tlsConfig,omitempty" yaml:"tls_config"`
  123. }
  124. func (a *AppConfig) Name() string {
  125. return fmt.Sprintf("%s-%s", a.AppName, a.AppEnv)
  126. }
  127. // CacheNameSpace 作为 Key 的前缀,用来区分不同环境,不同 APP 下的 Key,防止缓存干扰
  128. // 踩坑记录:
  129. //
  130. // 如果使用 fmt.Sprintf("%s-%s-%s", a.AppName, a.AppRole, a.AppEnv),例如 sayam-http-production, sayam-consumer-production
  131. // 会导致 从 role http 写入的 key,从 role consumer 中取不出来,反之亦然
  132. // 支持更多的key空间, 可以更灵活的定义ns
  133. func (a *AppConfig) CacheNameSpace() string {
  134. cacheKeySpace := ""
  135. if a.FarmName != "" {
  136. cacheKeySpace = fmt.Sprintf("%s-%s-%s", a.AppName, a.AppEnv, a.FarmName)
  137. } else {
  138. cacheKeySpace = fmt.Sprintf("%s-%s", a.AppName, a.AppEnv)
  139. }
  140. return cacheKeySpace
  141. }
  142. func Options() *AppConfig {
  143. return options
  144. }
  145. func init() {
  146. appEnv = strings.ToLower(os.Getenv("APP_ENVIRONMENT"))
  147. cfg := &AppConfig{}
  148. var err error
  149. initOnce.Do(func() {
  150. switch appEnv {
  151. case "test":
  152. err = Initialize("app.test.yaml", cfg)
  153. case "development":
  154. err = Initialize("app.develop.yaml", cfg)
  155. case "production":
  156. err = Initialize("app.production.yaml", cfg)
  157. default:
  158. panic("err confing")
  159. }
  160. if err != nil {
  161. panic(err)
  162. }
  163. cfg.JwtTokenKeyConfig = openPrivateKey()
  164. options = cfg
  165. })
  166. }