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存活检查(简单响应)
|
||||
* 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)')
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user