Files
learn-ai/backend/src/web/routes/health.ts
T
hughhuangandClaude Opus 4.5 b8791ee556 完善后端核心框架
- 添加错误处理中间件 (error-handler.ts)
- 添加请求日志中间件 (request-logger.ts)
- 添加健康检查路由 (health.ts)
- 更新 app.ts 集成中间件和数据库连接
- 添加优雅关闭时断开数据库连接

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 23:46:59 +08:00

44 lines
1016 B
TypeScript

import { FastifyInstance } from 'fastify';
import prisma from '../../config/database';
export async function healthRoutes(app: FastifyInstance): Promise<void> {
// 基础健康检查
app.get('/health', async (request, reply) => {
return {
success: true,
data: {
status: 'ok',
timestamp: new Date().toISOString(),
version: '1.0.0',
},
};
});
// 详细健康检查(包括数据库)
app.get('/health/detailed', async (request, reply) => {
const checks: Record<string, string> = {
server: 'ok',
};
// 检查数据库连接
try {
await prisma.$queryRaw`SELECT 1`;
checks.database = 'ok';
} catch (error) {
checks.database = 'error';
}
const allOk = Object.values(checks).every((v) => v === 'ok');
return {
success: allOk,
data: {
status: allOk ? 'ok' : 'degraded',
timestamp: new Date().toISOString(),
version: '1.0.0',
checks,
},
};
});
}