76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
import "reflect-metadata";
|
||
import { NestFactory } from "@nestjs/core";
|
||
import { Transport, MicroserviceOptions } from "@nestjs/microservices";
|
||
import { AppModule } from "./app.module.js";
|
||
import { GlobalErrorFilter } from "./shared/errors/global-error.filter.js";
|
||
import { initTracer, shutdownTracer } from "./shared/observability/tracer.js";
|
||
import { env } from "./config/env.js";
|
||
import { logger } from "./shared/observability/logger.js";
|
||
import { metricsRegistry } from "./shared/observability/metrics.js";
|
||
import { getJwtKeyPair } from "./config/jwt.js";
|
||
import type { Request, Response } from "express";
|
||
|
||
/**
|
||
* IAM 服务启动入口。
|
||
*
|
||
* 双入口(president §2.16):
|
||
* - HTTP server:env.PORT(3002),供 gateway 透传 + admin-portal 直连
|
||
* - gRPC server:env.GRPC_PORT(50052),供 BFF 聚合调用(I1 裁决)
|
||
*
|
||
* 启动顺序:
|
||
* 1. initTracer(OTel SDK)
|
||
* 2. 创建 NestApplication
|
||
* 3. 注册 GlobalErrorFilter
|
||
* 4. 启动 gRPC microservice(hybrid app)
|
||
* 5. 启动 HTTP server
|
||
* 6. 预加载 JWT 密钥对(确保文件可读)
|
||
*/
|
||
async function bootstrap(): Promise<void> {
|
||
initTracer();
|
||
|
||
// 预加载 JWT 密钥对(启动时即校验文件可读,避免运行时才发现配置错误)
|
||
getJwtKeyPair();
|
||
|
||
const app = await NestFactory.create(AppModule, {
|
||
logger: ["log", "error", "warn"],
|
||
});
|
||
|
||
app.useGlobalFilters(new GlobalErrorFilter());
|
||
app.enableShutdownHooks();
|
||
|
||
// gRPC microservice(端口 50052,I1 裁决)
|
||
app.connectMicroservice<MicroserviceOptions>({
|
||
transport: Transport.GRPC,
|
||
options: {
|
||
package: "next_edu_cloud.iam.v1",
|
||
protoPath: "proto/iam.proto",
|
||
url: `0.0.0.0:${env.GRPC_PORT}`,
|
||
},
|
||
});
|
||
|
||
// Prometheus 指标端点
|
||
app.getHttpAdapter().get("/metrics", async (_req: Request, res: Response) => {
|
||
res.set("Content-Type", metricsRegistry.contentType);
|
||
res.end(await metricsRegistry.metrics());
|
||
});
|
||
|
||
// 启动 hybrid app(HTTP + gRPC)
|
||
await app.startAllMicroservices();
|
||
await app.listen(env.PORT);
|
||
|
||
logger.info(
|
||
{ httpPort: env.PORT, grpcPort: env.GRPC_PORT },
|
||
"IAM service started (HTTP + gRPC dual entry)",
|
||
);
|
||
|
||
process.on("SIGTERM", async () => {
|
||
await app.close();
|
||
await shutdownTracer();
|
||
});
|
||
}
|
||
|
||
bootstrap().catch((err: unknown) => {
|
||
logger.error({ err }, "Failed to start IAM service");
|
||
process.exit(1);
|
||
});
|