package config import ( "kpt-grpc-demo/util/di" log "kpt-grpc-demo/util/logger" "os" "strings" "sync" ) const AppName = "kpt-ops-grpc" var ( Module = di.Provide(Options) options *AppConfig appEnv string initOnce sync.Once ) // AppConfig store all configuration options type AppConfig struct { AppName string `yaml:"app_name"` AppEnv string `yaml:"app_environment"` Debug bool `yaml:"debug" env:"APP_DEBUG"` HTTPServerAddr string `yaml:"http_server_addr" env:"HTTP_SERVER_ADDR"` HTTPMetricsAddr string `yaml:"http_metrics_addr" env:"HTTP_METRICS_ADDR"` GRPCServerAddr string `yaml:"grpc_server_addr" env:"GRPC_SERVER_ADDR"` GRPCMetricsAddr string `yaml:"grpc_metrics_addr" env:"GRPC_METRICS_ADDR"` // 数据库配置 额外加载文件部分 database.yaml StoreSetting StoreSetting `json:"storeSetting" yaml:"store"` // 第三方服务配置 ThirdServiceSetting ThirdServiceSetting `yaml:"third_svc_setting"` } type ThirdServiceSetting struct { // ====================== // gRPC Service // ====================== ArabServiceLB string `yaml:"arab_service_lb" env:"ARAB_SERVICE_LB"` BanglaServiceLB string `yaml:"bangla_service_lb" env:"BANGLA_SERVICE_LB"` UserUrumaLB string `yaml:"user_uruma_lb" env:"USER_URUMA_LB"` } // StoreSetting 数据库配置 type StoreSetting struct { // 开启 SyDb SQL 记录 ShowSQL bool `yaml:"show_sql" env:"STORE_SHOW_SQL"` KptEventDSNRW string `yaml:"kpt_event_rw" env:"LINGO_COURSE_DSN_RW"` KptEventDSNMigr string `yaml:"kpt_event_migr" env:"LINGO_COURSE_DSN_MIGR"` } func Options() *AppConfig { return options } func init() { appEnv = strings.ToLower(os.Getenv("APP_ENVIRONMENT")) cfg := &AppConfig{} var err error initOnce.Do(func() { switch appEnv { default: err = Initialize("app.test.yaml", cfg) log.SetLevel(log.ErrorLevel) case "development": err = Initialize("app.develop.yaml", cfg) case "production": err = Initialize("app.production.yaml", cfg) } if err != nil { log.Fatalf("%+v", err) } options = cfg }) }