diff --git a/backend/src/common/health/healthCheck.ts b/backend/src/common/health/healthCheck.ts index 1a986dea..f9e9c814 100644 --- a/backend/src/common/health/healthCheck.ts +++ b/backend/src/common/health/healthCheck.ts @@ -18,9 +18,10 @@ export interface HealthCheckResponse { /** * 注册健康检查路由 * - * 提供两个端点: - * 1. /health/liveness - SAE存活检查(简单响应) - * 2. /health/readiness - SAE就绪检查(检查依赖服务) + * 提供三个端点: + * 1. /health - 向后兼容的简化健康检查 + * 2. /health/liveness - SAE存活检查(简单响应) + * 3. /health/readiness - SAE就绪检查(检查依赖服务) * * @example * ```typescript @@ -31,6 +32,32 @@ export interface HealthCheckResponse { * ``` */ export async function registerHealthRoutes(app: FastifyInstance): Promise { + /** + * 简化健康检查(向后兼容) + * + * GET /health + */ + app.get('/health', async ( + _request: FastifyRequest, + reply: FastifyReply + ) => { + // 检查数据库连接 + let dbStatus = 'unknown' + try { + await prisma.$queryRaw`SELECT 1` + dbStatus = 'connected' + } catch { + dbStatus = 'disconnected' + } + + return reply.status(200).send({ + status: 'ok', + database: dbStatus, + timestamp: new Date().toISOString(), + uptime: process.uptime() + }) + }) + /** * 存活检查(Liveness Probe) * @@ -219,3 +246,4 @@ export async function registerHealthRoutes(app: FastifyInstance): Promise console.log(' - GET /health (detailed)') } + diff --git a/backend/src/index.ts b/backend/src/index.ts index 8ed8b9ec..3fbf5e1c 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -10,6 +10,8 @@ import knowledgeBaseRoutes from './legacy/routes/knowledgeBases.js'; import { chatRoutes } from './legacy/routes/chatRoutes.js'; import { batchRoutes } from './legacy/routes/batchRoutes.js'; import reviewRoutes from './legacy/routes/reviewRoutes.js'; +import { registerHealthRoutes } from './common/health/index.js'; +import { logger } from './common/logging/index.js'; // 全局处理BigInt序列化 @@ -52,24 +54,12 @@ await fastify.register(multipart, { console.log('✅ 文件上传插件已配置: 最大文件大小 10MB'); -// 健康检查路由 -fastify.get('/health', async () => { - // 检查数据库连接 - let dbStatus = 'unknown'; - try { - await prisma.$queryRaw`SELECT 1`; - dbStatus = 'connected'; - } catch { - dbStatus = 'disconnected'; - } - - return { - status: 'ok', - database: dbStatus, - timestamp: new Date().toISOString(), - uptime: process.uptime(), - }; -}); +// ============================================ +// 【平台基础设施】健康检查路由 +// ============================================ +// 注册健康检查路由(/health, /health/liveness, /health/readiness) +await registerHealthRoutes(fastify); +logger.info('✅ 健康检查路由已注册'); // API路由前缀 fastify.get('/api/v1', async () => {