123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- package config
- import (
- "crypto/rsa"
- "crypto/tls"
- "fmt"
- "os"
- "strings"
- "sync"
- "time"
- "gitee.com/xuyiping_admin/pkg/di"
- )
- var (
- Module = di.Provide(Options)
- options *AppConfig
- appEnv string
- initOnce sync.Once
- )
- // AppConfig store all configuration options
- type AppConfig struct {
- FarmName string `json:"farm_name"`
- AppName string `yaml:"app_name"`
- AppEnv string `yaml:"app_environment"`
- Debug bool `yaml:"debug" env:"APP_DEBUG"`
- HTTPServerAddr string `yaml:"http_server_addr" env:"HTTP_SERVER_ADDR"`
- // 数据库配置 额外加载文件部分 database.yaml
- StoreSetting StoreSetting `json:"storeSetting" yaml:"store"`
- // redis 配置
- RedisSetting RedisSetting `json:"RedisSetting" yaml:"redis_setting"`
- JwtSecret string `json:"jwtSecret" yaml:"jwt_secret"`
- ExcelSetting ExcelSetting `json:"excelSetting" yaml:"excel_setting"`
- WechatSetting WechatSetting `json:"wechatSetting" yaml:"wechat_setting"`
- JwtTokenKeyConfig JwtTokenKeyConfig `json:"jwtTokenKeyConfig"`
- JwtExpireTime int `json:"jwtExpireTime" yaml:"jwt_expire_time"`
- // asynq 相关配置
- SideWorkSetting SideWorkSetting `yaml:"side_work_setting"`
- CronSetting CronSetting `json:"cron_setting" yaml:"cron"`
- }
- type CronSetting struct {
- // 是否启动任务时先跑一次
- CrontabStartRun bool `yaml:"crontab_start_run"`
- // CRONTAB 表达式
- UpdateCowInfo string `yaml:"update_cow_info"`
- GenerateWorkOrder string `yaml:"generate_work_order"`
- ImmunizationPlan string `yaml:"immunization_plan"`
- SameTimePlan string `yaml:"same_time_plan"`
- }
- type JwtTokenKeyConfig struct {
- PrivateKey *rsa.PrivateKey `json:"privateKey"`
- PublicKey *rsa.PublicKey `json:"publicKey"`
- }
- type WechatSetting struct {
- Appid string `yaml:"appid"`
- Secret string `yaml:"secret"`
- }
- type ExcelSetting struct {
- SheetName string `yaml:"sheet_name"` // = "Sheet1" //默认Sheet名称
- Height float64 `yaml:"height"` // = 25.0 //默认行高度
- }
- // StoreSetting 数据库配置
- type StoreSetting struct {
- // 开启 SyDb SQL 记录
- DriverName string `yaml:"driver_name" json:"driver_name"`
- ShowSQL bool `yaml:"show_sql" json:"show_sql"`
- KptRW string `yaml:"kpt_rw" json:"kpt_rw"`
- KptMigr string `yaml:"kpt_migr" json:"kpt_migr"`
- }
- type RedisSetting struct {
- // sso 配置
- CacheRedis CacheRedisDB `json:"cache_redis" yaml:"cache_redis"`
- }
- type CacheRedisDB struct {
- Addr string `json:"addr" yaml:"addr"`
- DB int `json:"db" yaml:"db"`
- Requirepass string `json:"requirepass" yaml:"requirepass"`
- Expiry int `json:"expiry" yaml:"expiry"`
- }
- type SideWorkSetting struct {
- // Asynq 配置
- AsynqSetting AsynqSetting `json:"asynq_setting,omitempty" yaml:"asynq_setting"`
- }
- type AsynqSetting struct {
- Redis AsynqRedisSetting `json:"redis" yaml:"redis"`
- Queues map[string]int `json:"queues,omitempty" yaml:"queues"`
- Concurrency int `json:"concurrency,omitempty" yaml:"concurrency"`
- LogLevel int32 `json:"log_level,omitempty" yaml:"log_level"`
- }
- type AsynqRedisSetting struct {
- // Network type to use, either tcp or unix.
- // Default is tcp.
- Network string `json:"network,omitempty" yaml:"network"`
- // Redis server address in "host:port" format.
- Addr string `json:"addr,omitempty" yaml:"addr"`
- // Username to authenticate the current connection when Redis ACLs are used.
- // See: https://redis.io/commands/auth.
- Username string `json:"username,omitempty" yaml:"username"`
- // Password to authenticate the current connection.
- // See: https://redis.io/commands/auth.
- Password string `json:"password,omitempty" yaml:"password"`
- // Redis DB to select after connecting to a server.
- // See: https://redis.io/commands/select.
- DB int `json:"db,omitempty" yaml:"db"`
- // Dial timeout for establishing new connections.
- // Default is 5 seconds.
- DialTimeout time.Duration `json:"dialTimeout,omitempty" yaml:"dial_timeout"`
- // Timeout for socket reads.
- // If timeout is reached, read commands will fail with a timeout error
- // instead of blocking.
- //
- // Use value -1 for no timeout and 0 for default.
- // Default is 3 seconds.
- ReadTimeout time.Duration `json:"readTimeout,omitempty" yaml:"read_timeout"`
- // Timeout for socket writes.
- // If timeout is reached, write commands will fail with a timeout error
- // instead of blocking.
- //
- // Use value -1 for no timeout and 0 for default.
- // Default is ReadTimout.
- WriteTimeout time.Duration `json:"writeTimeout,omitempty" yaml:"write_timeout"`
- // Maximum number of socket connections.
- // Default is 10 connections per every CPU as reported by runtime.NumCPU.
- PoolSize int `json:"poolSize,omitempty" yaml:"pool_size"`
- // TLS Config used to connect to a server.
- // TLS will be negotiated only if this field is set.
- TLSConfig *tls.Config `json:"tlsConfig,omitempty" yaml:"tls_config"`
- }
- func (a *AppConfig) Name() string {
- return fmt.Sprintf("%s-%s", a.AppName, a.AppEnv)
- }
- // CacheNameSpace 作为 Key 的前缀,用来区分不同环境,不同 APP 下的 Key,防止缓存干扰
- // 踩坑记录:
- //
- // 如果使用 fmt.Sprintf("%s-%s-%s", a.AppName, a.AppRole, a.AppEnv),例如 sayam-http-production, sayam-consumer-production
- // 会导致 从 role http 写入的 key,从 role consumer 中取不出来,反之亦然
- // 支持更多的key空间, 可以更灵活的定义ns
- func (a *AppConfig) CacheNameSpace() string {
- cacheKeySpace := ""
- if a.FarmName != "" {
- cacheKeySpace = fmt.Sprintf("%s-%s-%s", a.AppName, a.AppEnv, a.FarmName)
- } else {
- cacheKeySpace = fmt.Sprintf("%s-%s", a.AppName, a.AppEnv)
- }
- return cacheKeySpace
- }
- func Options() *AppConfig {
- return options
- }
- func init() {
- appEnv = strings.ToLower(os.Getenv("APP_ENVIRONMENT"))
- cfg := &AppConfig{}
- var err error
- initOnce.Do(func() {
- switch appEnv {
- case "test":
- err = Initialize("app.test.yaml", cfg)
- case "development":
- err = Initialize("app.develop.yaml", cfg)
- case "production":
- err = Initialize("app.production.yaml", cfg)
- default:
- panic("err confing")
- }
- if err != nil {
- panic(err)
- }
- cfg.JwtTokenKeyConfig = openPrivateKey()
- options = cfg
- })
- }
|