app.go 2.3 KB

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