app.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package config
  2. import (
  3. "kpt-tmr-group/pkg/di"
  4. "os"
  5. "strings"
  6. "sync"
  7. )
  8. const AppName = "kptEvent"
  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. }
  24. // StoreSetting 数据库配置
  25. type StoreSetting struct {
  26. // 开启 SyDb SQL 记录
  27. DriverName string `yaml:"driver_name" json:"driver_name"`
  28. ShowSQL bool `yaml:"show_sql" env:"STORE_SHOW_SQL"`
  29. KptEventDSNRW string `yaml:"kpt_tmr_group_rw" env:"LINGO_COURSE_DSN_RW"`
  30. KptEventDSNMigr string `yaml:"kpt_tmr_group_migr" env:"LINGO_COURSE_DSN_MIGR"`
  31. }
  32. func Options() *AppConfig {
  33. return options
  34. }
  35. func init() {
  36. appEnv = strings.ToLower(os.Getenv("APP_ENVIRONMENT"))
  37. cfg := &AppConfig{}
  38. var err error
  39. initOnce.Do(func() {
  40. switch appEnv {
  41. default:
  42. err = Initialize("app.test.yaml", cfg)
  43. case "development":
  44. err = Initialize("app.develop.yaml", cfg)
  45. case "production":
  46. err = Initialize("app.production.yaml", cfg)
  47. }
  48. if err != nil {
  49. panic(err)
  50. }
  51. options = cfg
  52. })
  53. }