app.go 2.2 KB

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