import { RequestMethod, ValidationPipe } from "@nestjs/common"; import { ConfigService } from "@nestjs/config"; import { NestFactory } from "@nestjs/core"; import type { NestExpressApplication } from "@nestjs/platform-express"; import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger"; import { AppModule } from "./app.module"; import { appPackage } from "./common/app-package"; import { configureBodyParsers } from "./common/body-parsers"; import { TraceLogger } from "./common/trace-logger"; import { AppConfig } from "./config/app.config"; import { HttpExceptionFilter } from "./filters/http-exception.filter"; import { LoggingInterceptor } from "./interceptors/logging.interceptor"; import { TraceInterceptor } from "./interceptors/trace.interceptor"; // Guard against uncaught exceptions thrown inside Playwright event listener // callbacks (e.g. a bad waitForURL predicate). Without this, Node.js v15+ // crashes the process on any unhandled rejection or uncaught exception. const processLogger = new TraceLogger("Process"); process.on("uncaughtException", (err) => { processLogger.error(`Uncaught exception (process kept alive): ${err.message}`, err.stack); }); process.on("unhandledRejection", (reason) => { processLogger.error( `Unhandled promise rejection (process kept alive): ${ reason instanceof Error ? reason.message : String(reason) }`, ); }); async function bootstrap() { const { name: pkgName, version: pkgVersion } = appPackage(); const API_PREFIX = "api/v1"; const logger = new TraceLogger("Bootstrap"); const app = await NestFactory.create(AppModule, { logger: new TraceLogger("Bootstrap", { timestamp: true }), }); app.setGlobalPrefix(API_PREFIX, { exclude: [ { path: "health", method: RequestMethod.ALL }, { path: "healthz", method: RequestMethod.ALL }, { path: "mcp", method: RequestMethod.ALL }, ], }); configureBodyParsers(app); app.useGlobalPipes(new ValidationPipe({ transform: true })); app.useGlobalFilters(new HttpExceptionFilter()); app.useGlobalInterceptors(new TraceInterceptor(), new LoggingInterceptor()); const config = app.get>(ConfigService); const port = config.get("PORT"); const nodeEnv = config.get("NODE_ENV"); app.enableCors({ origin: ["http://localhost:5173", "http://127.0.0.1:5173"], methods: ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE", "OPTIONS"], credentials: true, }); const swaggerConfig = new DocumentBuilder() .setTitle(pkgName) .setDescription(`${pkgName} API`) .setVersion(pkgVersion) .build(); const document = SwaggerModule.createDocument(app, swaggerConfig); SwaggerModule.setup("docs", app, document); await app.listen(port); logger.log( `Application "${pkgName}" v${pkgVersion} running on port ${port} [${nodeEnv}]`, ); logger.log(`Swagger UI available at http://localhost:${port}/docs`); logger.log(`MCP endpoint available at http://localhost:${port}/mcp`); } bootstrap();