app.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package config
  2. import (
  3. "crypto/rsa"
  4. "os"
  5. "strings"
  6. "sync"
  7. "gitee.com/xuyiping_admin/pkg/di"
  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" json:"show_sql"`
  48. KptRW string `yaml:"kpt_rw" json:"kpt_rw"`
  49. KptMigr string `yaml:"kpt_migr" json:"kpt_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. case "test":
  71. err = Initialize("app.test.yaml", cfg)
  72. case "development":
  73. err = Initialize("app.develop.yaml", cfg)
  74. case "production":
  75. err = Initialize("app.production.yaml", cfg)
  76. default:
  77. panic("err confing")
  78. }
  79. if err != nil {
  80. panic(err)
  81. }
  82. cfg.JwtTokenKeyConfig = openPrivateKey()
  83. options = cfg
  84. })
  85. }