feat: 完成配置中心重构和命名规范优化
- 重构config层为配置中心架构,支持动态配置管理 - 统一core层命名规范(event-bus→event, circuit-breaker→breaker, domain-sdk→sdk) - 修复数据库连接配置路径问题 - 实现配置中心完整功能:系统配置、动态配置、配置验证、统计 - 优化目录结构,为微服务架构做准备 - 修复TypeScript编译错误和依赖注入问题
This commit is contained in:
84
wwjcloud/src/config/schemas/appSchema.ts
Normal file
84
wwjcloud/src/config/schemas/appSchema.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import Joi from 'joi';
|
||||
|
||||
/**
|
||||
* 应用配置验证模式
|
||||
*/
|
||||
export const AppConfigSchema = Joi.object({
|
||||
NODE_ENV: Joi.string().valid('development', 'test', 'production').required(),
|
||||
PORT: Joi.number().default(3000),
|
||||
APP_NAME: Joi.string().optional(),
|
||||
APP_VERSION: Joi.string().optional(),
|
||||
TZ: Joi.string().optional(),
|
||||
|
||||
// 数据库配置验证
|
||||
DB_HOST: Joi.string().required(),
|
||||
DB_PORT: Joi.number().required(),
|
||||
DB_USERNAME: Joi.string().required(),
|
||||
DB_PASSWORD: Joi.string().allow('').required(),
|
||||
DB_DATABASE: Joi.string().required(),
|
||||
DB_SYNC: Joi.boolean().optional(),
|
||||
DB_LOGGING: Joi.boolean().optional(),
|
||||
|
||||
// Redis配置验证
|
||||
REDIS_HOST: Joi.string().optional(),
|
||||
REDIS_PORT: Joi.number().optional(),
|
||||
REDIS_PASSWORD: Joi.string().allow('').optional(),
|
||||
REDIS_DB: Joi.number().optional(),
|
||||
REDIS_KEY_PREFIX: Joi.string().optional(),
|
||||
|
||||
// Kafka配置验证
|
||||
KAFKA_CLIENT_ID: Joi.string().optional(),
|
||||
KAFKA_BROKERS: Joi.string().optional(),
|
||||
KAFKA_GROUP_ID: Joi.string().optional(),
|
||||
KAFKA_TOPIC_PREFIX: Joi.string().optional(),
|
||||
|
||||
// JWT配置验证
|
||||
JWT_SECRET: Joi.string().required(),
|
||||
JWT_EXPIRES_IN: Joi.string().optional(),
|
||||
JWT_ALGORITHM: Joi.string().optional(),
|
||||
|
||||
// 缓存配置验证
|
||||
CACHE_TTL: Joi.number().optional(),
|
||||
CACHE_MAX_ITEMS: Joi.number().optional(),
|
||||
CACHE_PREFIX: Joi.string().optional(),
|
||||
|
||||
// 日志配置验证
|
||||
LOG_LEVEL: Joi.string().optional(),
|
||||
LOG_FORMAT: Joi.string().optional(),
|
||||
LOG_FILENAME: Joi.string().optional(),
|
||||
|
||||
// 上传配置验证
|
||||
UPLOAD_PATH: Joi.string().optional(),
|
||||
UPLOAD_MAX_SIZE: Joi.number().optional(),
|
||||
UPLOAD_ALLOWED_TYPES: Joi.string().optional(),
|
||||
|
||||
// 限流配置验证
|
||||
THROTTLE_TTL: Joi.number().optional(),
|
||||
THROTTLE_LIMIT: Joi.number().optional(),
|
||||
|
||||
// 第三方服务配置验证
|
||||
STORAGE_PROVIDER: Joi.string().optional(),
|
||||
STORAGE_CONFIG: Joi.string().optional(),
|
||||
PAYMENT_PROVIDER: Joi.string().valid('mock', 'alipay', 'wechat', 'stripe').optional(),
|
||||
PAYMENT_CONFIG: Joi.string().optional(),
|
||||
SMS_PROVIDER: Joi.string().optional(),
|
||||
SMS_CONFIG: Joi.string().optional(),
|
||||
}).unknown(true);
|
||||
|
||||
/**
|
||||
* 验证配置
|
||||
* @param config 配置对象
|
||||
* @returns 验证结果
|
||||
*/
|
||||
export function validateAppConfig(config: Record<string, any>) {
|
||||
const { error, value } = AppConfigSchema.validate(config, {
|
||||
allowUnknown: true,
|
||||
abortEarly: false
|
||||
});
|
||||
|
||||
if (error) {
|
||||
throw new Error(`应用配置验证失败: ${error.message}`);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
Reference in New Issue
Block a user