123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- 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
- )
- type AppConfig struct {
- FarmName string `yaml:"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"`
-
- StoreSetting StoreSetting `json:"storeSetting" yaml:"store"`
-
- 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"`
-
- SideWorkSetting SideWorkSetting `yaml:"side_work_setting"`
- CronSetting CronSetting `json:"cron_setting" yaml:"cron"`
- Mqtt MqttSetting `json:"mqtt"`
- }
- type CronSetting struct {
-
- CrontabStartRun bool `yaml:"crontab_start_run"`
-
- UpdateCowInfo string `yaml:"update_cow_info"`
- GenerateWorkOrder string `yaml:"generate_work_order"`
- ImmunizationPlan string `yaml:"immunization_plan"`
- SameTimePlan string `yaml:"same_time_plan"`
- UpdateSameTime string `yaml:"update_same_time"`
- SystemBasicCrontab string `yaml:"system_basic_crontab"`
- CowPregnant string `yaml:"cow_pregnant"`
- }
- 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"`
- Height float64 `yaml:"height"`
- }
- type StoreSetting struct {
-
- 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 {
-
- 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 {
-
- 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 string `json:"network,omitempty" yaml:"network"`
-
- Addr string `json:"addr,omitempty" yaml:"addr"`
-
-
- Username string `json:"username,omitempty" yaml:"username"`
-
-
- Password string `json:"password,omitempty" yaml:"password"`
-
-
- DB int `json:"db,omitempty" yaml:"db"`
-
-
- DialTimeout time.Duration `json:"dialTimeout,omitempty" yaml:"dial_timeout"`
-
-
-
-
-
-
- ReadTimeout time.Duration `json:"readTimeout,omitempty" yaml:"read_timeout"`
-
-
-
-
-
-
- WriteTimeout time.Duration `json:"writeTimeout,omitempty" yaml:"write_timeout"`
-
-
- PoolSize int `json:"poolSize,omitempty" yaml:"pool_size"`
-
-
- TLSConfig *tls.Config `json:"tlsConfig,omitempty" yaml:"tls_config"`
- }
- type MqttSetting struct {
- Broker string `json:"broker" yaml:"broker"`
- Port int `json:"port" yaml:"port"`
- UserName string `json:"username" yaml:"username"`
- Password string `json:"password" yaml:"password"`
- ClientId string `json:"client" yaml:"client_id"`
- Topic string `json:"topic" yaml:"topic"`
- Retain bool `json:"retain" yaml:"retain"`
- Qos int `json:"qos" yaml:"qos"`
- KeepAlive int `json:"keepAlive" yaml:"keep_alive"`
- ConnectTimeout int `json:"connectTimeout" yaml:"connect_timeout"`
- AutoReconnect bool `json:"autoReconnect" yaml:"auto_reconnect"`
- ReconnectInterval int `json:"reconnectInterval" yaml:"reconnect_interval"`
- WorkNumber int `json:"workNumber" yaml:"work_number"`
- }
- func (a *AppConfig) Name() string {
- return fmt.Sprintf("%s-%s", a.AppName, a.AppEnv)
- }
- 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
- })
- }
|