61 lines
1.8 KiB
TypeScript
61 lines
1.8 KiB
TypeScript
import 'reflect-metadata';
|
|
import { ValidationPipe } from '@nestjs/common';
|
|
import { NestFactory } from '@nestjs/core';
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
|
import { join } from 'path';
|
|
import { TypeOrmExceptionFilter } from './common/filters/typeorm-exception.filter';
|
|
import { AppModule } from './app.module';
|
|
|
|
function configureCors(app: Awaited<ReturnType<typeof NestFactory.create>>) {
|
|
const raw = process.env.CORS_ORIGINS?.trim();
|
|
if (raw) {
|
|
const origins = raw.split(',').map((o) => o.trim()).filter(Boolean);
|
|
app.enableCors({ origin: origins, credentials: true });
|
|
return;
|
|
}
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
app.enableCors({ origin: true, credentials: true });
|
|
}
|
|
}
|
|
|
|
async function bootstrap() {
|
|
const app = await NestFactory.create<NestExpressApplication>(AppModule);
|
|
const uploadDir = process.env.UPLOAD_DIR ?? 'uploads';
|
|
app.useStaticAssets(join(process.cwd(), uploadDir), { prefix: '/uploads' });
|
|
configureCors(app);
|
|
app.useGlobalPipes(
|
|
new ValidationPipe({
|
|
forbidNonWhitelisted: true,
|
|
transform: true,
|
|
whitelist: true,
|
|
}),
|
|
);
|
|
app.useGlobalFilters(new TypeOrmExceptionFilter());
|
|
|
|
const swaggerConfig = new DocumentBuilder()
|
|
.setTitle('Montime API')
|
|
.setDescription('Local services super app API')
|
|
.setVersion('1.0')
|
|
.addBearerAuth(
|
|
{
|
|
type: 'http',
|
|
scheme: 'bearer',
|
|
bearerFormat: 'JWT',
|
|
},
|
|
'bearer',
|
|
)
|
|
.addSecurityRequirements('bearer')
|
|
.build();
|
|
|
|
const swaggerDocument = SwaggerModule.createDocument(app, swaggerConfig);
|
|
SwaggerModule.setup('docs', app, swaggerDocument, {
|
|
swaggerOptions: {
|
|
persistAuthorization: true,
|
|
},
|
|
});
|
|
|
|
await app.listen(process.env.PORT ?? 3000);
|
|
}
|
|
bootstrap();
|