app.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package config
  2. import (
  3. "kpt-grpc-demo/util/di"
  4. log "kpt-grpc-demo/util/logger"
  5. "os"
  6. "strings"
  7. "sync"
  8. )
  9. const AppName = "kpt-ops-grpc"
  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. HTTPMetricsAddr string `yaml:"http_metrics_addr" env:"HTTP_METRICS_ADDR"`
  23. GRPCServerAddr string `yaml:"grpc_server_addr" env:"GRPC_SERVER_ADDR"`
  24. GRPCMetricsAddr string `yaml:"grpc_metrics_addr" env:"GRPC_METRICS_ADDR"`
  25. // 数据库配置 额外加载文件部分 database.yaml
  26. StoreSetting StoreSetting `json:"storeSetting" yaml:"store"`
  27. // 第三方服务配置
  28. ThirdServiceSetting ThirdServiceSetting `yaml:"third_svc_setting"`
  29. }
  30. type ThirdServiceSetting struct {
  31. // ======================
  32. // gRPC Service
  33. // ======================
  34. ArabServiceLB string `yaml:"arab_service_lb" env:"ARAB_SERVICE_LB"`
  35. BanglaServiceLB string `yaml:"bangla_service_lb" env:"BANGLA_SERVICE_LB"`
  36. UserUrumaLB string `yaml:"user_uruma_lb" env:"USER_URUMA_LB"`
  37. }
  38. // StoreSetting 数据库配置
  39. type StoreSetting struct {
  40. // 开启 SyDb SQL 记录
  41. ShowSQL bool `yaml:"show_sql" env:"STORE_SHOW_SQL"`
  42. KptEventDSNRW string `yaml:"kpt_event_rw" env:"LINGO_COURSE_DSN_RW"`
  43. KptEventDSNMigr string `yaml:"kpt_event_migr" env:"LINGO_COURSE_DSN_MIGR"`
  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. log.SetLevel(log.ErrorLevel)
  57. case "development":
  58. err = Initialize("app.develop.yaml", cfg)
  59. case "production":
  60. err = Initialize("app.production.yaml", cfg)
  61. }
  62. if err != nil {
  63. log.Fatalf("%+v", err)
  64. }
  65. options = cfg
  66. })
  67. }