project init
This commit is contained in:
60
apps/api/src/main.ts
Normal file
60
apps/api/src/main.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
||||
import helmet from 'helmet';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
const configService = app.get(ConfigService);
|
||||
|
||||
// Security
|
||||
app.use(helmet());
|
||||
|
||||
// CORS (веб на порту 3125)
|
||||
const allowedOrigins = [
|
||||
configService.get('NEXT_PUBLIC_APP_URL'),
|
||||
'http://localhost:3125',
|
||||
'http://localhost:3000',
|
||||
].filter(Boolean) as string[];
|
||||
app.enableCors({
|
||||
origin: allowedOrigins.length ? allowedOrigins : 'http://localhost:3125',
|
||||
credentials: true,
|
||||
});
|
||||
|
||||
// Global prefix
|
||||
app.setGlobalPrefix('api');
|
||||
|
||||
// Validation
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
whitelist: true,
|
||||
forbidNonWhitelisted: true,
|
||||
transform: true,
|
||||
transformOptions: {
|
||||
enableImplicitConversion: true,
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
// Swagger
|
||||
if (configService.get('NODE_ENV') !== 'production') {
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle('CourseCraft API')
|
||||
.setDescription('AI-powered course creation platform API')
|
||||
.setVersion('1.0')
|
||||
.addBearerAuth()
|
||||
.build();
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
SwaggerModule.setup('docs', app, document);
|
||||
}
|
||||
|
||||
const port = configService.get('PORT') || 3001;
|
||||
await app.listen(port);
|
||||
|
||||
console.log(`🚀 API is running on: http://localhost:${port}/api`);
|
||||
console.log(`📚 Swagger docs: http://localhost:${port}/docs`);
|
||||
}
|
||||
|
||||
bootstrap();
|
||||
Reference in New Issue
Block a user