fix(backend): Resolve duplicate /health route registration

- Move /health route into registerHealthRoutes function for backward compatibility
- Update index.ts to only call registerHealthRoutes once
- Now provides 3 endpoints: /health, /health/liveness, /health/readiness

Fixes: FastifyError Method 'GET' already declared for route '/health'
This commit is contained in:
2025-11-18 07:39:47 +08:00
parent 31d555f7bb
commit 61a45aa917
2 changed files with 39 additions and 21 deletions

View File

@@ -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<void> {
/**
* 简化健康检查(向后兼容)
*
* 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<void>
console.log(' - GET /health (detailed)')
}

View File

@@ -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 () => {