- allow the vite dev server origins in development and the compose port origins in production; CORS_ORIGINS overrides both - set NODE_ENV=production in the runtime docker image - document both settings in .env.example and the development docs - bump version to 1.19.3
81 lines
3.0 KiB
TypeScript
81 lines
3.0 KiB
TypeScript
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 { corsOrigins } from "./common/cors";
|
|
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<NestExpressApplication>(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<AppConfig, true>>(ConfigService);
|
|
const port = config.get("PORT");
|
|
const nodeEnv = config.get("NODE_ENV");
|
|
|
|
app.enableCors({
|
|
origin: corsOrigins(nodeEnv, config.get("CORS_ORIGINS")),
|
|
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();
|