app.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package config
  2. import (
  3. "go.uber.org/zap"
  4. "os"
  5. "strings"
  6. "sync"
  7. "gitee.com/xuyiping_admin/pkg/di"
  8. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  9. )
  10. const AppName = "kptEvent"
  11. var (
  12. Module = di.Provide(Options)
  13. options *AppConfig
  14. appEnv string
  15. initOnce sync.Once
  16. )
  17. // AppConfig store all configuration options
  18. type AppConfig struct {
  19. AppName string `yaml:"app_name"`
  20. AppEnv string `yaml:"app_environment"`
  21. Debug bool `yaml:"debug" env:"APP_DEBUG"`
  22. HTTPServerAddr string `yaml:"http_server_addr" env:"HTTP_SERVER_ADDR"`
  23. // 数据库配置 额外加载文件部分 database.yaml
  24. StoreSetting StoreSetting `json:"storeSetting" yaml:"store"`
  25. }
  26. // StoreSetting 数据库配置
  27. type StoreSetting struct {
  28. // 开启 SyDb SQL 记录
  29. DriverName string `yaml:"driver_name" json:"driver_name"`
  30. ShowSQL bool `yaml:"show_sql" env:"STORE_SHOW_SQL"`
  31. KptEventDSNRW string `yaml:"kpt_event_rw" env:"LINGO_COURSE_DSN_RW"`
  32. KptEventDSNMigr string `yaml:"kpt_event_migr" env:"LINGO_COURSE_DSN_MIGR"`
  33. }
  34. func Options() *AppConfig {
  35. return options
  36. }
  37. func init() {
  38. appEnv = strings.ToLower(os.Getenv("APP_ENVIRONMENT"))
  39. cfg := &AppConfig{}
  40. var err error
  41. initOnce.Do(func() {
  42. switch appEnv {
  43. default:
  44. err = Initialize("app.test.yaml", cfg)
  45. case "development":
  46. err = Initialize("app.develop.yaml", cfg)
  47. case "production":
  48. err = Initialize("app.production.yaml", cfg)
  49. }
  50. if err != nil {
  51. zaplog.Error("%+v", zap.Any("project init", err))
  52. }
  53. options = cfg
  54. })
  55. }