app.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package config
  2. import (
  3. "crypto/rsa"
  4. "gitee.com/xuyiping_admin/pkg/di"
  5. "os"
  6. "strings"
  7. "sync"
  8. )
  9. var (
  10. Module = di.Provide(Options)
  11. options *AppConfig
  12. appEnv string
  13. initOnce sync.Once
  14. )
  15. // AppConfig store all configuration options
  16. type AppConfig struct {
  17. AppName string `yaml:"app_name"`
  18. AppEnv string `yaml:"app_environment"`
  19. Debug bool `yaml:"debug" env:"APP_DEBUG"`
  20. HTTPServerAddr string `yaml:"http_server_addr" env:"HTTP_SERVER_ADDR"`
  21. // 数据库配置 额外加载文件部分 database.yaml
  22. StoreSetting StoreSetting `json:"storeSetting" yaml:"store"`
  23. // redis 配置
  24. RedisSetting RedisSetting `json:"RedisSetting" yaml:"redis_setting"`
  25. JwtSecret string `json:"jwtSecret" yaml:"jwt_secret"`
  26. ExcelSetting ExcelSetting `json:"excelSetting" yaml:"excel_setting"`
  27. WechatSetting WechatSetting `json:"wechatSetting" yaml:"wechat_setting"`
  28. JwtTokenKeyConfig JwtTokenKeyConfig `json:"jwtTokenKeyConfig"`
  29. JwtExpireTime int `json:"jwtExpireTime" yaml:"jwt_expire_time"`
  30. }
  31. type JwtTokenKeyConfig struct {
  32. PrivateKey *rsa.PrivateKey `json:"privateKey"`
  33. PublicKey *rsa.PublicKey `json:"publicKey"`
  34. }
  35. type WechatSetting struct {
  36. Appid string `yaml:"appid"`
  37. Secret string `yaml:"secret"`
  38. }
  39. type ExcelSetting struct {
  40. SheetName string `yaml:"sheet_name"` // = "Sheet1" //默认Sheet名称
  41. Height float64 `yaml:"height"` // = 25.0 //默认行高度
  42. }
  43. // StoreSetting 数据库配置
  44. type StoreSetting struct {
  45. // 开启 SyDb SQL 记录
  46. DriverName string `yaml:"driver_name" json:"driver_name"`
  47. ShowSQL bool `yaml:"show_sql" env:"STORE_SHOW_SQL"`
  48. KptEventDSNRW string `yaml:"kpt_tmr_group_rw" env:"LINGO_COURSE_DSN_RW"`
  49. KptEventDSNMigr string `yaml:"kpt_tmr_group_migr" env:"LINGO_COURSE_DSN_MIGR"`
  50. }
  51. type RedisSetting struct {
  52. // sso 配置
  53. SSOCache SSOCache `json:"SSOCache" yaml:"sso_cache"`
  54. }
  55. type SSOCache struct {
  56. Addr string `json:"addr" yaml:"addr"`
  57. Requirepass string `json:"requirepass" yaml:"requirepass"`
  58. DB int `json:"DB" yaml:"DB"`
  59. Expiry int `json:"expiry" yaml:"expiry"`
  60. }
  61. func Options() *AppConfig {
  62. return options
  63. }
  64. func init() {
  65. appEnv = strings.ToLower(os.Getenv("APP_ENVIRONMENT"))
  66. cfg := &AppConfig{}
  67. var err error
  68. initOnce.Do(func() {
  69. switch appEnv {
  70. default:
  71. err = Initialize("app.test.yaml", cfg)
  72. case "develop":
  73. err = Initialize("app.develop.yaml", cfg)
  74. case "production":
  75. err = Initialize("app.production.yaml", cfg)
  76. }
  77. if err != nil {
  78. panic(err)
  79. }
  80. cfg.JwtTokenKeyConfig = openPrivateKey()
  81. options = cfg
  82. })
  83. }