Files
liqa/server/src/main.ts
T
Andrii Arsenin 2afa07902e chore: upgrade to node 24 and read app version from workspace root
- base the docker image on node:24-slim and require node >=24 via
  engines in the root package.json
- read app name and version from the workspace root package.json for
  config, swagger, startup log and mcp server info; drop the server
  workspace version
- bind test http servers to 127.0.0.1 so tests pass in containers
  without ipv6 loopback
- bump version to 1.19.1
2026-09-30 20:57:33 +03:00

80 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 { 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: ["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();