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

145 lines
3.7 KiB
TypeScript

import { Injectable, Logger } from '@nestjs/common';
export interface DynamicConfig {
key: string;
value: any;
description?: string;
type?: 'string' | 'number' | 'boolean' | 'json';
category?: string;
isPublic?: boolean;
createdAt?: Date;
updatedAt?: Date;
}
@Injectable()
export class DynamicConfigService {
private readonly logger = new Logger(DynamicConfigService.name);
private readonly configs = new Map<string, DynamicConfig>();
constructor() {
// 初始化一些默认配置
this.initializeDefaultConfigs();
}
private initializeDefaultConfigs() {
const defaultConfigs: DynamicConfig[] = [
{
key: 'system.maintenance',
value: false,
description: '系统维护模式',
type: 'boolean',
category: 'system',
isPublic: true,
createdAt: new Date(),
updatedAt: new Date(),
},
{
key: 'system.debug',
value: process.env.NODE_ENV === 'development',
description: '调试模式',
type: 'boolean',
category: 'system',
isPublic: false,
createdAt: new Date(),
updatedAt: new Date(),
},
];
defaultConfigs.forEach(config => {
this.configs.set(config.key, config);
});
}
/**
* 获取配置
*/
async getConfig(key: string): Promise<DynamicConfig | null> {
return this.configs.get(key) || null;
}
/**
* 获取配置列表
*/
async getConfigList(category?: string): Promise<DynamicConfig[]> {
const configs = Array.from(this.configs.values());
if (category) {
return configs.filter(config => config.category === category);
}
return configs;
}
/**
* 设置配置
*/
async setConfig(
key: string,
value: any,
options: {
description?: string;
type?: 'string' | 'number' | 'boolean' | 'json';
category?: string;
isPublic?: boolean;
} = {}
): Promise<void> {
const existingConfig = this.configs.get(key);
const now = new Date();
const config: DynamicConfig = {
key,
value,
description: options.description || existingConfig?.description,
type: options.type || this.inferType(value) || existingConfig?.type || 'string',
category: options.category || existingConfig?.category || 'general',
isPublic: options.isPublic !== undefined ? options.isPublic : existingConfig?.isPublic ?? true,
createdAt: existingConfig?.createdAt || now,
updatedAt: now,
};
this.configs.set(key, config);
this.logger.log(`动态配置已更新: ${key} = ${value}`);
}
/**
* 删除配置
*/
async deleteConfig(key: string): Promise<void> {
if (this.configs.has(key)) {
this.configs.delete(key);
this.logger.log(`动态配置已删除: ${key}`);
}
}
/**
* 推断值类型
*/
private inferType(value: any): 'string' | 'number' | 'boolean' | 'json' {
if (typeof value === 'string') return 'string';
if (typeof value === 'number') return 'number';
if (typeof value === 'boolean') return 'boolean';
if (typeof value === 'object') return 'json';
return 'string';
}
/**
* 获取配置统计
*/
async getConfigStats() {
const configs = Array.from(this.configs.values());
const categories = [...new Set(configs.map(c => c.category))];
return {
total: configs.length,
categories: categories.length,
public: configs.filter(c => c.isPublic).length,
private: configs.filter(c => !c.isPublic).length,
byCategory: categories.reduce((acc, category) => {
if (category) {
acc[category] = configs.filter(c => c.category === category).length;
}
return acc;
}, {} as Record<string, number>),
};
}
}