Files
Edu/apps/admin-portal/src/app/api/ready/route.ts
SpecialX b3511910d1 feat(admin-portal): 完整实现 admin-portal 管理端微前端
包含 src 全部实现、Dockerfile、配置文件等
2026-07-10 19:09:12 +08:00

48 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 就绪检查 - Readiness
*
* GET /api/ready
* 检查依赖服务可达性(开发期直接返回 200
*/
import { NextResponse } from "next/server";
export const dynamic = "force-dynamic";
export async function GET(): Promise<NextResponse> {
// 开发期MSW 启用时直接就绪
if (process.env.NEXT_PUBLIC_API_MOCKING === "enabled") {
return NextResponse.json({
status: "ready",
service: "admin-portal",
dependencies: { gateway: "mocked" },
timestamp: Date.now(),
});
}
// 生产:检查 api-gateway 可达性
const gatewayUrl = process.env.API_GATEWAY_URL ?? "http://localhost:8080";
try {
const res = await fetch(`${gatewayUrl}/healthz`, {
signal: AbortSignal.timeout(2000),
});
if (!res.ok) throw new Error(`gateway status ${res.status}`);
return NextResponse.json({
status: "ready",
service: "admin-portal",
dependencies: { gateway: "ok" },
timestamp: Date.now(),
});
} catch (err) {
return NextResponse.json(
{
status: "not_ready",
service: "admin-portal",
dependencies: { gateway: "unreachable" },
error: err instanceof Error ? err.message : String(err),
timestamp: Date.now(),
},
{ status: 503 },
);
}
}