app.go 6.2 KB

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