Files
dsc-api/src/main.ts
T
2026-02-02 08:56:02 +03:30

49 lines
1.9 KiB
TypeScript
Executable File

import compression from "@fastify/compress";
import { ClassSerializerInterceptor, Logger, ValidationPipe } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { NestFactory, Reflector } from "@nestjs/core";
import { FastifyAdapter, NestFastifyApplication } from "@nestjs/platform-fastify";
import { AppModule } from "./app.module";
import { getSwaggerDocument } from "./configs/swagger.config";
import { HttpExceptionFilter } from "./core/exceptions/http.exceptions";
import { PaginationInterceptor } from "./core/interceptors/pagination.interceptor";
import { ResponseInterceptor } from "./core/interceptors/response.interceptor";
async function bootstrap() {
const logger = new Logger("APP");
const fastifyAdapter = new FastifyAdapter({
bodyLimit: 10 * 1024 * 1024,
trustProxy: true,
});
fastifyAdapter.getInstance().addContentTypeParser("multipart/form-data", (_request, _payload, done) => {
done(null);
});
const app = await NestFactory.create<NestFastifyApplication>(AppModule, fastifyAdapter);
await app.register(compression as any, { encodings: ["gzip", "deflate"] });
app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true }));
app.useGlobalInterceptors(new ResponseInterceptor(), new PaginationInterceptor(), new ClassSerializerInterceptor(app.get(Reflector)));
app.useGlobalFilters(new HttpExceptionFilter());
app.enableCors({
origin: true,
methods: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
credentials: true,
optionsSuccessStatus: 204,
});
const configService = app.get<ConfigService>(ConfigService);
getSwaggerDocument(app);
const PORT = configService.getOrThrow<number>("PORT");
await app.listen(PORT, "0.0.0.0", () => {
logger.log(`Server running at http://localhost:${PORT}`);
logger.log(`Swagger is serving at http://localhost:${PORT}/api-docs`);
});
}
bootstrap();