Files
Edu/services/parent-bff/src/main.ts
SpecialX 2229309a1e feat: initialize parent-bff service with full core features
add complete parent-bff implementation including:
- GraphQL endpoint with depth/cost validation
- ChildGuard越权校验 with redis cache and singleflight
- parallel orchestration with partial failure fallback
- three-level cache fallback strategy (Redis + LRU + downstream)
- Kafka consumer for cache invalidation and notification push
- opossum circuit breaker for downstream services
- Prometheus metrics and SLO alerts
- Helm chart for k8s deployment with multi-environment configs
- Grafana dashboard for observability
- complete unit and integration tests
2026-07-10 18:49:06 +08:00

63 lines
2.1 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.
import "reflect-metadata";
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module.js";
import { GlobalErrorFilter } from "./shared/errors/global-error.filter.js";
import { initTracer, shutdownTracer } from "./shared/observability/tracer.js";
import { logger } from "./shared/observability/logger.js";
import { env } from "./config/env.js";
import { metricsRegistry } from "./shared/observability/metrics.js";
import { closeRedisClient } from "./shared/cache/redis.client.js";
import type { Request, Response } from "express";
/**
* parent-bff 启动入口(对齐 02-architecture-design.md §6.7 优雅关闭)。
*
* 启动顺序:
* 1. initTracerOTel SDK可选未配置 endpoint 时跳过)
* 2. NestFactory.create + GlobalErrorFilter
* 3. /metrics 端点Prometheus 抓取)
* 4. app.listen(3010)
*
* SIGTERM 关闭顺序P4.1 基础版P5+ 补充 Kafka consumer
* 1. app.close()(拒绝新请求 + 等待 in-flight
* 2. shutdownTracer()flush OTel span
* 3. process.exit(0)(由 NestJS enableShutdownHooks 触发)
*/
async function bootstrap(): Promise<void> {
initTracer();
const app = await NestFactory.create(AppModule, {
logger: ["log", "error", "warn"],
});
app.useGlobalFilters(new GlobalErrorFilter());
app.enableShutdownHooks();
// Prometheus 指标端点:不鉴权,供 Prometheus 抓取。
app.getHttpAdapter().get("/metrics", async (_req: Request, res: Response) => {
res.set("Content-Type", metricsRegistry.contentType);
res.end(await metricsRegistry.metrics());
});
// CORS默认允许 parent-portallocalhost:4002
app.enableCors({
origin: env.CORS_ORIGINS.split(",").map((o) => o.trim()),
credentials: true,
});
await app.listen(env.PORT);
logger.info({ port: env.PORT, devMode: env.DEV_MODE }, "Parent BFF started");
process.on("SIGTERM", async () => {
logger.info("Parent BFF received SIGTERM, shutting down...");
await app.close();
await closeRedisClient();
await shutdownTracer();
});
}
bootstrap().catch((err: unknown) => {
logger.error({ err }, "Failed to start Parent BFF");
process.exit(1);
});