Files
dmail-api/src/main.ts
T
2025-07-02 21:05:13 +03:30

65 lines
2.3 KiB
TypeScript

import { MikroORM } from "@mikro-orm/core";
import { Logger, ValidationPipe } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { NestFactory } 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/filters/http-exception.filters";
import { MailServerExceptionFilter } from "./core/filters/mail-server-exception.filter";
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);
});
fastifyAdapter.getInstance().addContentTypeParser("text/plain", (_request, _payload, done) => {
done(null);
});
const app = await NestFactory.create<NestFastifyApplication>(AppModule, fastifyAdapter);
app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true, forbidNonWhitelisted: true }));
app.useGlobalInterceptors(new ResponseInterceptor(), new PaginationInterceptor());
app.useGlobalFilters(new MailServerExceptionFilter(), 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 orm = app.get(MikroORM);
if (process.env.NODE_ENV !== "production") {
await app.get(MikroORM).getSchemaGenerator().ensureDatabase();
await orm.getSchemaGenerator().updateSchema();
}
const PORT = configService.getOrThrow<number>("PORT");
await app.listen(PORT, "0.0.0.0", () => {
logger.log(`Application is running on: http://localhost:${PORT}`);
logger.log(`Swagger documentation: http://localhost:${PORT}/api-docs`);
});
}
bootstrap().catch((error) => {
const logger = new Logger("Bootstrap");
logger.error("Failed to start application", error);
process.exit(1);
});