完善后端核心框架
- 添加错误处理中间件 (error-handler.ts) - 添加请求日志中间件 (request-logger.ts) - 添加健康检查路由 (health.ts) - 更新 app.ts 集成中间件和数据库连接 - 添加优雅关闭时断开数据库连接 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
773fa369cd
commit
b8791ee556
4 files changed
+146
-25
No files matched your search
@@ -0,0 +1,43 @@
|
||||
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,
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user