Files
tehran_tala/apps/api/src/main.ts
T

41 lines
1.6 KiB
TypeScript
Raw Normal View History

2026-09-09 06:46:27 +03:30
import 'reflect-metadata';
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { extname, join } from 'node:path';
import type { Response } from 'express';
2026-09-09 06:46:27 +03:30
import { AppModule } from './app.module';
import { prepareDatabase } from './prisma/bootstrap';
2026-09-09 06:46:27 +03:30
import { PrismaExceptionFilter } from './prisma/prisma-exception.filter';
// serve-static's bundled `mime` version predates .avif, so it falls back to
// application/octet-stream — browsers still render the image (they sniff
// content), but a strict client or an <img> under some CSPs would not.
const EXTRA_MIME_TYPES: Record<string, string> = {
'.avif': 'image/avif',
};
2026-09-09 06:46:27 +03:30
async function bootstrap() {
await prepareDatabase();
const app = await NestFactory.create<NestExpressApplication>(AppModule);
2026-09-09 06:46:27 +03:30
app.setGlobalPrefix('api');
app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
app.useGlobalFilters(new PrismaExceptionFilter());
app.enableCors();
// Uploaded files, mounted OUTSIDE the /api prefix so the URLs stored on
// UploadedFile.url ("/uploads/<name>") resolve directly.
app.useStaticAssets(process.env.UPLOAD_DIR ?? join(process.cwd(), 'storage', 'uploads'), {
prefix: '/uploads/',
index: false,
dotfiles: 'deny',
setHeaders: (res: Response, path: string) => {
const mime = EXTRA_MIME_TYPES[extname(path).toLowerCase()];
if (mime) res.setHeader('Content-Type', mime);
},
});
// Bind all interfaces explicitly so the container is reachable from nginx.
await app.listen(process.env.PORT ?? 3000, '0.0.0.0');
2026-09-09 06:46:27 +03:30
}
bootstrap();