app.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package config
  2. import (
  3. "fmt"
  4. "kpt-tmr-group/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. }
  29. type WechatSetting struct {
  30. Appid string `yaml:"appid"`
  31. Secret string `yaml:"secret"`
  32. }
  33. type ExcelSetting struct {
  34. SheetName string `yaml:"sheet_name"` // = "Sheet1" //默认Sheet名称
  35. Height float64 `yaml:"height"` // = 25.0 //默认行高度
  36. }
  37. // StoreSetting 数据库配置
  38. type StoreSetting struct {
  39. // 开启 SyDb SQL 记录
  40. DriverName string `yaml:"driver_name" json:"driver_name"`
  41. ShowSQL bool `yaml:"show_sql" env:"STORE_SHOW_SQL"`
  42. KptEventDSNRW string `yaml:"kpt_tmr_group_rw" env:"LINGO_COURSE_DSN_RW"`
  43. KptEventDSNMigr string `yaml:"kpt_tmr_group_migr" env:"LINGO_COURSE_DSN_MIGR"`
  44. }
  45. type RedisSetting struct {
  46. // sso 配置
  47. SSOCache SSOCache `json:"SSOCache" yaml:"sso_cache"`
  48. }
  49. type SSOCache struct {
  50. Addr string `json:"addr" yaml:"addr"`
  51. Requirepass string `json:"requirepass" yaml:"requirepass"`
  52. DB int `json:"DB" yaml:"DB"`
  53. Expiry int `json:"expiry" yaml:"expiry"`
  54. }
  55. func Options() *AppConfig {
  56. return options
  57. }
  58. func init() {
  59. appEnv = strings.ToLower(os.Getenv("APP_ENVIRONMENT"))
  60. fmt.Println("=====APP_ENVIRONMENT======", appEnv)
  61. cfg := &AppConfig{}
  62. var err error
  63. initOnce.Do(func() {
  64. switch appEnv {
  65. default:
  66. err = Initialize("app.test.yaml", cfg)
  67. case "develop":
  68. err = Initialize("app.develop.yaml", cfg)
  69. case "production":
  70. err = Initialize("app.production.yaml", cfg)
  71. }
  72. if err != nil {
  73. panic(err)
  74. }
  75. options = cfg
  76. })
  77. }