app.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // redis 配置
  24. RedisSetting RedisSetting `json:"RedisSetting" yaml:"redis_setting"`
  25. JwtSecret string `json:"jwtSecret" yaml:"jwt_secret"`
  26. }
  27. // StoreSetting 数据库配置
  28. type StoreSetting struct {
  29. // 开启 SyDb SQL 记录
  30. DriverName string `yaml:"driver_name" json:"driver_name"`
  31. ShowSQL bool `yaml:"show_sql" env:"STORE_SHOW_SQL"`
  32. KptEventDSNRW string `yaml:"kpt_tmr_group_rw" env:"LINGO_COURSE_DSN_RW"`
  33. KptEventDSNMigr string `yaml:"kpt_tmr_group_migr" env:"LINGO_COURSE_DSN_MIGR"`
  34. }
  35. type RedisSetting struct {
  36. // sso 配置
  37. SSOCache SSOCache `json:"SSOCache" yaml:"sso_cache"`
  38. }
  39. type SSOCache struct {
  40. Addr string `json:"addr" yaml:"addr"`
  41. Requirepass string `json:"requirepass" yaml:"requirepass"`
  42. DB int `json:"DB" yaml:"DB"`
  43. Expiry int `json:"expiry" yaml:"expiry"`
  44. }
  45. func Options() *AppConfig {
  46. return options
  47. }
  48. func init() {
  49. appEnv = strings.ToLower(os.Getenv("APP_ENVIRONMENT"))
  50. cfg := &AppConfig{}
  51. var err error
  52. initOnce.Do(func() {
  53. switch appEnv {
  54. default:
  55. err = Initialize("app.test.yaml", cfg)
  56. case "development":
  57. err = Initialize("app.develop.yaml", cfg)
  58. case "production":
  59. err = Initialize("app.production.yaml", cfg)
  60. }
  61. if err != nil {
  62. panic(err)
  63. }
  64. options = cfg
  65. })
  66. }