database.ts 导出 db 常量替代 getDb() env.ts JWT_SECRET 改 optional 并新增 DEV_MODE kafka.ts connectKafka 加 try/catch 不阻塞启动 main.ts 去全局前缀 connectKafka 改非阻塞 app.module 移除未用模块加 HealthModule controller 路由去前缀去 UseGuards 从 x-user-id 读身份 service datetime ISO 字符串转 Date 修复 drizzle 错误 修正相对 import 路径 health/lifecycle 改用 Drizzle 原生查询 新增 core-edu-init.sql 初始化 4 张表 端到端验证: exams/homework/grades 全部 201/200 Outbox 事件正确写入 core_edu_outbox 表
45 lines
1.0 KiB
TypeScript
45 lines
1.0 KiB
TypeScript
import { Controller, Get, HttpException, HttpStatus } from "@nestjs/common";
|
|
import { sql } from "drizzle-orm";
|
|
import { db } from "../../config/database.js";
|
|
|
|
const SERVICE_NAME = "core-edu";
|
|
|
|
@Controller()
|
|
export class HealthController {
|
|
@Get("healthz")
|
|
liveness(): { status: string; service: string; timestamp: string } {
|
|
return {
|
|
status: "ok",
|
|
service: SERVICE_NAME,
|
|
timestamp: new Date().toISOString(),
|
|
};
|
|
}
|
|
|
|
@Get("readyz")
|
|
async readiness(): Promise<{
|
|
status: string;
|
|
service: string;
|
|
timestamp: string;
|
|
}> {
|
|
try {
|
|
await db.execute(sql`SELECT 1`);
|
|
return {
|
|
status: "ok",
|
|
service: SERVICE_NAME,
|
|
timestamp: new Date().toISOString(),
|
|
};
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
{
|
|
status: "error",
|
|
service: SERVICE_NAME,
|
|
timestamp: new Date().toISOString(),
|
|
error:
|
|
error instanceof Error ? error.message : "database unreachable",
|
|
},
|
|
HttpStatus.SERVICE_UNAVAILABLE,
|
|
);
|
|
}
|
|
}
|
|
}
|