import { existsSync } from 'node:fs'; import path from 'node:path'; import { NestFactory } from '@nestjs/core'; import { ValidationPipe } from '@nestjs/common'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { json, urlencoded } from 'express'; import { AppModule } from './app.module'; import { applyPendingMigrations } from './apply-migrations'; import { runBundledSeed } from './run-seed'; function swaggerUiAssetsPath() { const bundled = path.join(process.cwd(), 'runtime', 'swagger-ui'); if (existsSync(path.join(bundled, 'swagger-ui.css'))) { return bundled; } return path.join(process.cwd(), 'node_modules', 'swagger-ui-dist'); } async function bootstrap() { if (process.env.NODE_ENV === 'production') { await applyPendingMigrations(); if (process.env.RUN_SEED === 'true' || process.env.RUN_SEED === 'force') { await runBundledSeed(); } } const app = await NestFactory.create(AppModule); // تصاویر چک و رسید به‌صورت base64 داخل JSON می‌آیند و از سقف پیش‌فرض ۱۰۰KB رد می‌شوند const bodyLimit = process.env.BODY_LIMIT || '25mb'; app.use(json({ limit: bodyLimit })); app.use(urlencoded({ extended: true, limit: bodyLimit })); // پشت LiteSpeed روی /server → GLOBAL_PREFIX=server/api const globalPrefix = process.env.GLOBAL_PREFIX || 'server/api'; app.setGlobalPrefix(globalPrefix); app.enableCors({ origin: true, credentials: true, }); app.useGlobalPipes( new ValidationPipe({ whitelist: true, transform: true, forbidNonWhitelisted: true, transformOptions: { enableImplicitConversion: true }, }), ); const swaggerConfig = new DocumentBuilder() .setTitle('Fabric Trade API') .setDescription('API سرور تجارت پارچه') .setVersion('1.0') .addBearerAuth() .build(); const document = SwaggerModule.createDocument(app, swaggerConfig); SwaggerModule.setup(`${globalPrefix}/docs`, app, document, { customSwaggerUiPath: swaggerUiAssetsPath(), }); const port = Number(process.env.PORT) || 3000; await app.listen(port); console.log( `Fabric Trade API listening on http://localhost:${port}/${globalPrefix}`, ); console.log(`Swagger docs at http://localhost:${port}/${globalPrefix}/docs`); } void bootstrap();