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