- register list_snippets, get_snippet, create_snippet, update_snippet, delete_snippet - inject SnippetService into McpService and import SnippetModule into McpModule - cover all five tools with integration tests including error cases
76 lines
2.8 KiB
TypeScript
76 lines
2.8 KiB
TypeScript
import { RequestMethod, ValidationPipe } from "@nestjs/common";
|
|
import { ConfigService } from "@nestjs/config";
|
|
import { NestFactory } from "@nestjs/core";
|
|
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
|
|
import { name as pkgName, version as pkgVersion } from "../package.json";
|
|
import { AppModule } from "./app.module";
|
|
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 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 },
|
|
],
|
|
});
|
|
|
|
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();
|