feat(platform): Implement platform infrastructure with cloud-native support
- Add storage service (LocalAdapter + OSSAdapter stub) - Add database connection pool with graceful shutdown - Add logging system with winston (JSON format) - Add environment config management - Add async job queue (MemoryQueue + DatabaseQueue stub) - Add cache service (MemoryCache + RedisCache stub) - Add health check endpoints for SAE - Add monitoring metrics for DB, memory, API Key Features: - Zero-code switching between local and cloud environments - Adapter pattern for multi-environment support - Backward compatible with legacy modules - Ready for Aliyun Serverless deployment Related: Platform Infrastructure Planning (docs/09-鏋舵瀯瀹炴柦/04-骞冲彴鍩虹璁炬柦瑙勫垝.md)
This commit is contained in:
@@ -1,37 +1,167 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
// 创建Prisma Client实例
|
||||
/**
|
||||
* 云原生数据库连接池配置
|
||||
*
|
||||
* 核心目标:
|
||||
* - 防止Serverless扩容导致连接数超限
|
||||
* - 优雅关闭连接
|
||||
* - 支持本地和云端环境
|
||||
*
|
||||
* 连接池计算公式:
|
||||
* connectionLimit = Math.floor(RDS_MAX_CONNECTIONS / MAX_INSTANCES) - 预留
|
||||
*
|
||||
* 示例:
|
||||
* - RDS: 400最大连接
|
||||
* - SAE: 最多20个实例
|
||||
* - 每实例连接数: 400 / 20 = 20 - 预留 = 18
|
||||
*
|
||||
* 环境变量:
|
||||
* - DATABASE_URL: 数据库连接URL(Prisma标准)
|
||||
* - DB_MAX_CONNECTIONS: RDS最大连接数(默认400)
|
||||
* - MAX_INSTANCES: SAE最大实例数(默认20)
|
||||
* - NODE_ENV: development | production
|
||||
*/
|
||||
|
||||
/**
|
||||
* 计算连接池大小(工具函数)
|
||||
*
|
||||
* ⚠️ 注意:Prisma不直接支持connectionLimit参数
|
||||
* 需要在DATABASE_URL中配置:
|
||||
* postgresql://user:pass@host:5432/db?connection_limit=20&pool_timeout=10
|
||||
*
|
||||
* 本函数用于计算推荐的connection_limit值
|
||||
*/
|
||||
export function calculateConnectionLimit(): number {
|
||||
const dbMaxConnections = Number(process.env.DB_MAX_CONNECTIONS) || 400
|
||||
const maxInstances = Number(process.env.MAX_INSTANCES) || 20
|
||||
const reservedConnections = 10 // 预留给管理任务和其他服务
|
||||
|
||||
// 计算每实例可用连接数
|
||||
const connectionsPerInstance = Math.floor(dbMaxConnections / maxInstances) - reservedConnections
|
||||
|
||||
// 确保至少有5个连接
|
||||
const connectionLimit = Math.max(connectionsPerInstance, 5)
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.log(`[Database] Connection pool calculation:`)
|
||||
console.log(` - DB max connections: ${dbMaxConnections}`)
|
||||
console.log(` - Max instances: ${maxInstances}`)
|
||||
console.log(` - Recommended connections per instance: ${connectionLimit}`)
|
||||
console.log(` 💡 Add to DATABASE_URL: ?connection_limit=${connectionLimit}`)
|
||||
}
|
||||
|
||||
return connectionLimit
|
||||
}
|
||||
|
||||
// 创建Prisma Client实例(全局单例)
|
||||
export const prisma = new PrismaClient({
|
||||
log: process.env.NODE_ENV === 'development' ? ['query', 'info', 'warn', 'error'] : ['error'],
|
||||
});
|
||||
log: process.env.NODE_ENV === 'development'
|
||||
? ['query', 'info', 'warn', 'error']
|
||||
: ['error'],
|
||||
|
||||
// ⭐ 云原生连接池配置
|
||||
datasources: {
|
||||
db: {
|
||||
url: process.env.DATABASE_URL,
|
||||
},
|
||||
},
|
||||
|
||||
// Prisma 不直接支持 connectionLimit,但可以通过 DATABASE_URL 配置
|
||||
// 示例:postgresql://user:password@host:5432/db?connection_limit=20
|
||||
})
|
||||
|
||||
// 数据库连接测试
|
||||
/**
|
||||
* 数据库连接测试
|
||||
*/
|
||||
export async function testDatabaseConnection(): Promise<boolean> {
|
||||
try {
|
||||
await prisma.$connect();
|
||||
console.log('✅ 数据库连接成功!');
|
||||
await prisma.$connect()
|
||||
console.log('✅ 数据库连接成功!')
|
||||
|
||||
// 获取数据库信息
|
||||
const result = await prisma.$queryRaw<Array<{ version: string }>>`SELECT version()`;
|
||||
console.log('📊 数据库版本:', result[0]?.version.split(' ')[0], result[0]?.version.split(' ')[1]);
|
||||
const result = await prisma.$queryRaw<Array<{ version: string }>>`SELECT version()`
|
||||
console.log('📊 数据库版本:', result[0]?.version.split(' ')[0], result[0]?.version.split(' ')[1])
|
||||
|
||||
return true;
|
||||
// 获取当前连接数
|
||||
const connectionCount = await getDatabaseConnectionCount()
|
||||
console.log('📊 当前数据库连接数:', connectionCount)
|
||||
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('❌ 数据库连接失败:', error);
|
||||
return false;
|
||||
console.error('❌ 数据库连接失败:', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// 优雅关闭数据库连接
|
||||
export async function closeDatabaseConnection() {
|
||||
await prisma.$disconnect();
|
||||
console.log('👋 数据库连接已关闭');
|
||||
/**
|
||||
* 获取当前数据库连接数
|
||||
* 用于监控和告警
|
||||
*/
|
||||
export async function getDatabaseConnectionCount(): Promise<number> {
|
||||
try {
|
||||
const result = await prisma.$queryRaw<Array<{ count: bigint }>>`
|
||||
SELECT count(*) as count
|
||||
FROM pg_stat_activity
|
||||
WHERE datname = current_database()
|
||||
`
|
||||
return Number(result[0]?.count || 0)
|
||||
} catch (error) {
|
||||
console.error('❌ 获取数据库连接数失败:', error)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// 进程退出时关闭连接
|
||||
/**
|
||||
* 优雅关闭数据库连接
|
||||
*
|
||||
* 在以下情况下调用:
|
||||
* - 进程正常退出
|
||||
* - 收到SIGTERM信号(Serverless实例停止)
|
||||
* - 收到SIGINT信号(Ctrl+C)
|
||||
*/
|
||||
export async function closeDatabaseConnection(): Promise<void> {
|
||||
try {
|
||||
console.log('[Database] Closing connections...')
|
||||
await prisma.$disconnect()
|
||||
console.log('[Database] ✅ 连接已关闭')
|
||||
} catch (error) {
|
||||
console.error('[Database] ❌ 关闭连接失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// ⭐ 云原生:优雅关闭逻辑
|
||||
let isShuttingDown = false
|
||||
|
||||
/**
|
||||
* 处理优雅关闭
|
||||
*/
|
||||
async function gracefulShutdown(signal: string): Promise<void> {
|
||||
if (isShuttingDown) {
|
||||
console.log(`[Database] Already shutting down, ignoring ${signal}`)
|
||||
return
|
||||
}
|
||||
|
||||
isShuttingDown = true
|
||||
console.log(`[Database] Received ${signal}, shutting down gracefully...`)
|
||||
|
||||
try {
|
||||
await closeDatabaseConnection()
|
||||
process.exit(0)
|
||||
} catch (error) {
|
||||
console.error('[Database] Error during shutdown:', error)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// 监听进程信号
|
||||
process.on('SIGTERM', () => gracefulShutdown('SIGTERM')) // Serverless实例停止
|
||||
process.on('SIGINT', () => gracefulShutdown('SIGINT')) // Ctrl+C
|
||||
process.on('beforeExit', async () => {
|
||||
await closeDatabaseConnection();
|
||||
});
|
||||
if (!isShuttingDown) {
|
||||
await closeDatabaseConnection()
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,65 +1,239 @@
|
||||
import dotenv from 'dotenv';
|
||||
import path from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
import dotenv from 'dotenv'
|
||||
import path from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = path.dirname(__filename)
|
||||
|
||||
// 加载.env文件
|
||||
dotenv.config({ path: path.join(__dirname, '../../.env') });
|
||||
/**
|
||||
* 云原生环境配置管理
|
||||
*
|
||||
* 设计原则:
|
||||
* - ✅ 本地开发:从.env文件加载
|
||||
* - ✅ 云端部署:从SAE环境变量加载
|
||||
* - ✅ 统一配置管理,避免散落各处
|
||||
* - ✅ 启动时验证必需配置
|
||||
*
|
||||
* 环境变量优先级:
|
||||
* 1. 系统环境变量(最高优先级,云端部署)
|
||||
* 2. .env文件(本地开发)
|
||||
* 3. 默认值(兜底)
|
||||
*/
|
||||
|
||||
// 只在非生产环境加载.env文件
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
dotenv.config({ path: path.join(__dirname, '../../.env') })
|
||||
console.log('[Config] Loaded .env file for development')
|
||||
}
|
||||
|
||||
export const config = {
|
||||
// 服务器配置
|
||||
// ==================== 应用配置 ====================
|
||||
|
||||
/** 服务端口 */
|
||||
port: parseInt(process.env.PORT || '3001', 10),
|
||||
|
||||
/** 服务主机 */
|
||||
host: process.env.HOST || '0.0.0.0',
|
||||
|
||||
/** 运行环境 */
|
||||
nodeEnv: process.env.NODE_ENV || 'development',
|
||||
logLevel: process.env.LOG_LEVEL || 'info',
|
||||
|
||||
/** 日志级别 */
|
||||
logLevel: process.env.LOG_LEVEL || (process.env.NODE_ENV === 'production' ? 'info' : 'debug'),
|
||||
|
||||
/** 服务名称 */
|
||||
serviceName: process.env.SERVICE_NAME || 'aiclinical-backend',
|
||||
|
||||
// 数据库配置
|
||||
// ==================== 数据库配置 ====================
|
||||
|
||||
/** 数据库连接URL */
|
||||
databaseUrl: process.env.DATABASE_URL || 'postgresql://postgres:postgres@localhost:5432/ai_clinical',
|
||||
|
||||
/** RDS最大连接数(云原生配置) */
|
||||
dbMaxConnections: parseInt(process.env.DB_MAX_CONNECTIONS || '400', 10),
|
||||
|
||||
/** SAE最大实例数(云原生配置) */
|
||||
maxInstances: parseInt(process.env.MAX_INSTANCES || '20', 10),
|
||||
|
||||
// Redis配置
|
||||
// ==================== 存储配置(平台基础设施)====================
|
||||
|
||||
/** 存储类型:local | oss */
|
||||
storageType: process.env.STORAGE_TYPE || 'local',
|
||||
|
||||
/** 本地存储目录 */
|
||||
localStorageDir: process.env.LOCAL_STORAGE_DIR || 'uploads',
|
||||
|
||||
/** 本地存储URL前缀 */
|
||||
localStorageUrl: process.env.LOCAL_STORAGE_URL || 'http://localhost:3001/uploads',
|
||||
|
||||
/** 阿里云OSS地域 */
|
||||
ossRegion: process.env.OSS_REGION,
|
||||
|
||||
/** 阿里云OSS Bucket名称 */
|
||||
ossBucket: process.env.OSS_BUCKET,
|
||||
|
||||
/** 阿里云OSS AccessKey ID */
|
||||
ossAccessKeyId: process.env.OSS_ACCESS_KEY_ID,
|
||||
|
||||
/** 阿里云OSS AccessKey Secret */
|
||||
ossAccessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
|
||||
|
||||
// ==================== 缓存配置(平台基础设施)====================
|
||||
|
||||
/** 缓存类型:memory | redis */
|
||||
cacheType: process.env.CACHE_TYPE || 'memory',
|
||||
|
||||
/** Redis主机 */
|
||||
redisHost: process.env.REDIS_HOST,
|
||||
|
||||
/** Redis端口 */
|
||||
redisPort: parseInt(process.env.REDIS_PORT || '6379', 10),
|
||||
|
||||
/** Redis密码 */
|
||||
redisPassword: process.env.REDIS_PASSWORD,
|
||||
|
||||
/** Redis数据库索引 */
|
||||
redisDb: parseInt(process.env.REDIS_DB || '0', 10),
|
||||
|
||||
/** Redis URL(兼容旧配置) */
|
||||
redisUrl: process.env.REDIS_URL || 'redis://localhost:6379',
|
||||
|
||||
// JWT配置
|
||||
jwtSecret: process.env.JWT_SECRET || 'your-secret-key-change-in-production',
|
||||
jwtExpiresIn: process.env.JWT_EXPIRES_IN || '7d',
|
||||
// ==================== 任务队列配置(平台基础设施)====================
|
||||
|
||||
/** 任务队列类型:memory | database */
|
||||
queueType: process.env.QUEUE_TYPE || 'memory',
|
||||
|
||||
// LLM API配置
|
||||
// ==================== 安全配置 ====================
|
||||
|
||||
/** JWT密钥 */
|
||||
jwtSecret: process.env.JWT_SECRET || 'your-secret-key-change-in-production',
|
||||
|
||||
/** JWT过期时间 */
|
||||
jwtExpiresIn: process.env.JWT_EXPIRES_IN || '7d',
|
||||
|
||||
/** CORS允许的源 */
|
||||
corsOrigin: process.env.CORS_ORIGIN || 'http://localhost:5173',
|
||||
|
||||
// ==================== LLM API配置 ====================
|
||||
|
||||
/** DeepSeek API Key */
|
||||
deepseekApiKey: process.env.DEEPSEEK_API_KEY || '',
|
||||
|
||||
/** DeepSeek Base URL */
|
||||
deepseekBaseUrl: process.env.DEEPSEEK_BASE_URL || 'https://api.deepseek.com',
|
||||
|
||||
dashscopeApiKey: process.env.DASHSCOPE_API_KEY || '', // 用于Qwen模型
|
||||
/** 通义千问 API Key */
|
||||
dashscopeApiKey: process.env.DASHSCOPE_API_KEY || '',
|
||||
|
||||
/** Gemini API Key */
|
||||
geminiApiKey: process.env.GEMINI_API_KEY || '',
|
||||
|
||||
// CloseAI配置(代理OpenAI和Claude)
|
||||
/** CloseAI API Key(代理OpenAI和Claude) */
|
||||
closeaiApiKey: process.env.CLOSEAI_API_KEY || '',
|
||||
|
||||
/** CloseAI OpenAI Base URL */
|
||||
closeaiOpenaiBaseUrl: process.env.CLOSEAI_OPENAI_BASE_URL || 'https://api.openai-proxy.org/v1',
|
||||
|
||||
/** CloseAI Claude Base URL */
|
||||
closeaiClaudeBaseUrl: process.env.CLOSEAI_CLAUDE_BASE_URL || 'https://api.openai-proxy.org/anthropic',
|
||||
|
||||
// Dify配置
|
||||
// ==================== Dify配置 ====================
|
||||
|
||||
/** Dify API Key */
|
||||
difyApiKey: process.env.DIFY_API_KEY || '',
|
||||
|
||||
/** Dify API URL */
|
||||
difyApiUrl: process.env.DIFY_API_URL || 'http://localhost/v1',
|
||||
|
||||
// 文件上传配置
|
||||
// ==================== 文件上传配置(Legacy兼容)====================
|
||||
|
||||
/** 文件上传大小限制 */
|
||||
uploadMaxSize: parseInt(process.env.UPLOAD_MAX_SIZE || '10485760', 10), // 10MB
|
||||
|
||||
/** 文件上传目录(Legacy兼容,新模块使用storage) */
|
||||
uploadDir: process.env.UPLOAD_DIR || './uploads',
|
||||
|
||||
// CORS配置
|
||||
corsOrigin: process.env.CORS_ORIGIN || 'http://localhost:5173',
|
||||
};
|
||||
|
||||
// 验证必需的环境变量
|
||||
export function validateEnv(): void {
|
||||
const requiredVars = ['DATABASE_URL'];
|
||||
const missing = requiredVars.filter(v => !process.env[v]);
|
||||
// ==================== 功能开关(平台基础设施)====================
|
||||
|
||||
if (missing.length > 0) {
|
||||
console.warn(`Warning: Missing environment variables: ${missing.join(', ')}`);
|
||||
/** 启用的模块列表(逗号分隔) */
|
||||
enabledModules: process.env.ENABLED_MODULES?.split(',').map(m => m.trim()) || [],
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证必需的环境变量
|
||||
*
|
||||
* 在应用启动时调用,确保关键配置存在
|
||||
*/
|
||||
export function validateEnv(): void {
|
||||
const errors: string[] = []
|
||||
const warnings: string[] = []
|
||||
|
||||
// ========== 必需配置验证 ==========
|
||||
|
||||
if (!process.env.DATABASE_URL) {
|
||||
errors.push('DATABASE_URL is required')
|
||||
}
|
||||
|
||||
// 检查LLM API Keys
|
||||
if (!config.deepseekApiKey && !config.dashscopeApiKey) {
|
||||
console.warn('Warning: No LLM API keys configured. At least one of DEEPSEEK_API_KEY or DASHSCOPE_API_KEY should be set.');
|
||||
// ========== 云原生配置验证 ==========
|
||||
|
||||
// 如果使用OSS,验证OSS配置
|
||||
if (config.storageType === 'oss') {
|
||||
if (!config.ossRegion) errors.push('OSS_REGION is required when STORAGE_TYPE=oss')
|
||||
if (!config.ossBucket) errors.push('OSS_BUCKET is required when STORAGE_TYPE=oss')
|
||||
if (!config.ossAccessKeyId) errors.push('OSS_ACCESS_KEY_ID is required when STORAGE_TYPE=oss')
|
||||
if (!config.ossAccessKeySecret) errors.push('OSS_ACCESS_KEY_SECRET is required when STORAGE_TYPE=oss')
|
||||
}
|
||||
|
||||
// 如果使用Redis,验证Redis配置
|
||||
if (config.cacheType === 'redis') {
|
||||
if (!config.redisHost && !config.redisUrl) {
|
||||
warnings.push('REDIS_HOST or REDIS_URL should be set when CACHE_TYPE=redis')
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 安全配置验证 ==========
|
||||
|
||||
if (config.nodeEnv === 'production') {
|
||||
if (config.jwtSecret === 'your-secret-key-change-in-production') {
|
||||
errors.push('JWT_SECRET must be changed in production')
|
||||
}
|
||||
}
|
||||
|
||||
// ========== LLM配置验证 ==========
|
||||
|
||||
if (!config.deepseekApiKey && !config.dashscopeApiKey && !config.closeaiApiKey) {
|
||||
warnings.push(
|
||||
'No LLM API keys configured. At least one of DEEPSEEK_API_KEY, DASHSCOPE_API_KEY, or CLOSEAI_API_KEY should be set.'
|
||||
)
|
||||
}
|
||||
|
||||
// ========== 输出验证结果 ==========
|
||||
|
||||
if (errors.length > 0) {
|
||||
console.error('❌ [Config] Environment validation failed:')
|
||||
errors.forEach(err => console.error(` - ${err}`))
|
||||
throw new Error('Environment validation failed. Please check configuration.')
|
||||
}
|
||||
|
||||
if (warnings.length > 0) {
|
||||
console.warn('⚠️ [Config] Environment validation warnings:')
|
||||
warnings.forEach(warn => console.warn(` - ${warn}`))
|
||||
}
|
||||
|
||||
// 成功
|
||||
if (errors.length === 0 && warnings.length === 0) {
|
||||
console.log('✅ [Config] Environment validation passed')
|
||||
}
|
||||
|
||||
// 输出关键配置(脱敏)
|
||||
console.log('[Config] Application configuration:')
|
||||
console.log(` - Environment: ${config.nodeEnv}`)
|
||||
console.log(` - Port: ${config.port}`)
|
||||
console.log(` - Storage: ${config.storageType}`)
|
||||
console.log(` - Cache: ${config.cacheType}`)
|
||||
console.log(` - Queue: ${config.queueType}`)
|
||||
console.log(` - Log Level: ${config.logLevel}`)
|
||||
if (config.enabledModules.length > 0) {
|
||||
console.log(` - Enabled Modules: ${config.enabledModules.join(', ')}`)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user