app.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package config
  2. import (
  3. "kpt-event/util/di"
  4. log "kpt-event/util/logger"
  5. "os"
  6. "strings"
  7. "sync"
  8. )
  9. const AppName = "kptEvent"
  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. }
  25. // StoreSetting 数据库配置
  26. type StoreSetting struct {
  27. // 开启 SyDb SQL 记录
  28. ShowSQL bool `yaml:"show_sql" env:"STORE_SHOW_SQL"`
  29. KptEventDSNRW string `yaml:"kpt_event_rw" env:"LINGO_COURSE_DSN_RW"`
  30. KptEventDSNMigr string `yaml:"kpt_event_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. log.SetLevel(log.ErrorLevel)
  44. case "development":
  45. err = Initialize("app.develop.yaml", cfg)
  46. case "production":
  47. err = Initialize("app.production.yaml", cfg)
  48. }
  49. if err != nil {
  50. log.Fatalf("%+v", err)
  51. }
  52. options = cfg
  53. })
  54. }