Files
wwjcloud/wwjcloud/src/config/controllers/configController.ts
万物街 2084711030 feat: 完成配置中心重构和命名规范优化
- 重构config层为配置中心架构,支持动态配置管理
- 统一core层命名规范(event-bus→event, circuit-breaker→breaker, domain-sdk→sdk)
- 修复数据库连接配置路径问题
- 实现配置中心完整功能:系统配置、动态配置、配置验证、统计
- 优化目录结构,为微服务架构做准备
- 修复TypeScript编译错误和依赖注入问题
2025-08-28 05:19:14 +08:00

156 lines
5.2 KiB
TypeScript

import { Controller, Get, Post, Put, Delete, Body, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../common/auth/guards/JwtAuthGuard';
import { RolesGuard } from '../../common/auth/guards/RolesGuard';
import { Roles } from '../../common/auth/decorators/RolesDecorator';
import { ConfigCenterService } from '../services/configCenterService';
import { DynamicConfigService } from '../services/dynamicConfigService';
import { ConfigValidationService } from '../services/configValidationService';
@ApiTags('配置管理')
@Controller('adminapi/config')
@UseGuards(JwtAuthGuard, RolesGuard)
export class ConfigController {
constructor(
private configCenterService: ConfigCenterService,
private dynamicConfigService: DynamicConfigService,
private configValidationService: ConfigValidationService,
) {}
@Get('system')
@Roles('admin')
@ApiOperation({ summary: '获取系统配置' })
@ApiResponse({ status: 200, description: '获取系统配置成功' })
async getSystemConfig() {
return {
app: this.configCenterService.getAppConfig(),
database: this.configCenterService.getDatabaseConfig(),
redis: this.configCenterService.getRedisConfig(),
kafka: this.configCenterService.getKafkaConfig(),
jwt: this.configCenterService.getJwtConfig(),
cache: this.configCenterService.getCacheConfig(),
logging: this.configCenterService.getLoggingConfig(),
upload: this.configCenterService.getUploadConfig(),
throttle: this.configCenterService.getThrottleConfig(),
thirdParty: this.configCenterService.getThirdPartyConfig(),
};
}
@Get('dynamic')
@Roles('admin')
@ApiOperation({ summary: '获取动态配置列表' })
@ApiResponse({ status: 200, description: '获取动态配置列表成功' })
async getDynamicConfigs(@Query('category') category?: string) {
return await this.dynamicConfigService.getConfigList(category);
}
@Get('dynamic/:key')
@Roles('admin')
@ApiOperation({ summary: '获取动态配置' })
@ApiResponse({ status: 200, description: '获取动态配置成功' })
async getDynamicConfig(@Param('key') key: string) {
return await this.dynamicConfigService.getConfig(key);
}
@Post('dynamic')
@Roles('admin')
@ApiOperation({ summary: '创建动态配置' })
@ApiResponse({ status: 201, description: '创建动态配置成功' })
async createDynamicConfig(@Body() config: {
key: string;
value: any;
description?: string;
type?: 'string' | 'number' | 'boolean' | 'json';
category?: string;
isPublic?: boolean;
}) {
await this.dynamicConfigService.setConfig(
config.key,
config.value,
{
description: config.description,
type: config.type,
category: config.category,
isPublic: config.isPublic,
}
);
return { message: '配置创建成功' };
}
@Put('dynamic/:key')
@Roles('admin')
@ApiOperation({ summary: '更新动态配置' })
@ApiResponse({ status: 200, description: '更新动态配置成功' })
async updateDynamicConfig(
@Param('key') key: string,
@Body() config: {
value: any;
description?: string;
type?: 'string' | 'number' | 'boolean' | 'json';
category?: string;
isPublic?: boolean;
}
) {
await this.dynamicConfigService.setConfig(
key,
config.value,
{
description: config.description,
type: config.type,
category: config.category,
isPublic: config.isPublic,
}
);
return { message: '配置更新成功' };
}
@Delete('dynamic/:key')
@Roles('admin')
@ApiOperation({ summary: '删除动态配置' })
@ApiResponse({ status: 200, description: '删除动态配置成功' })
async deleteDynamicConfig(@Param('key') key: string) {
await this.dynamicConfigService.deleteConfig(key);
return { message: '配置删除成功' };
}
@Get('validate')
@Roles('admin')
@ApiOperation({ summary: '验证配置' })
@ApiResponse({ status: 200, description: '配置验证成功' })
async validateConfig() {
return await this.configValidationService.validateAll();
}
@Get('metadata')
@Roles('admin')
@ApiOperation({ summary: '获取配置元数据' })
@ApiResponse({ status: 200, description: '获取配置元数据成功' })
async getConfigMetadata() {
return this.configCenterService.getConfigMetadata();
}
@Get('suggestions')
@Roles('admin')
@ApiOperation({ summary: '获取配置建议' })
@ApiResponse({ status: 200, description: '获取配置建议成功' })
async getConfigSuggestions() {
return this.configValidationService.getSuggestions();
}
@Post('refresh-cache')
@Roles('admin')
@ApiOperation({ summary: '刷新配置缓存' })
@ApiResponse({ status: 200, description: '配置缓存刷新成功' })
async refreshConfigCache() {
this.configCenterService.refreshCache();
return { message: '配置缓存已刷新' };
}
@Get('stats')
@Roles('admin')
@ApiOperation({ summary: '获取配置统计信息' })
@ApiResponse({ status: 200, description: '获取配置统计信息成功' })
async getConfigStats() {
return this.configCenterService.getConfigStats();
}
}