mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-21 03:10:50 +00:00
38
projects/sandbox/src/main.ts
Normal file
38
projects/sandbox/src/main.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
|
||||
import { AppModule } from './app.module';
|
||||
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
||||
import { HttpExceptionFilter } from './http-exception.filter';
|
||||
import { ResponseInterceptor } from './response';
|
||||
|
||||
async function bootstrap(port: number) {
|
||||
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
|
||||
|
||||
// 使用全局异常过滤器
|
||||
app.useGlobalFilters(new HttpExceptionFilter());
|
||||
|
||||
app.useGlobalInterceptors(new ResponseInterceptor());
|
||||
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle('Cats example')
|
||||
.setDescription('The cats API description')
|
||||
.setVersion('1.0')
|
||||
.addTag('cats')
|
||||
.build();
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
SwaggerModule.setup('api', app, document);
|
||||
|
||||
try {
|
||||
await app.listen(port, '0.0.0.0');
|
||||
console.log(`Application is running on: ${await app.getUrl()}`);
|
||||
} catch (error) {
|
||||
if (error.code === 'EADDRINUSE') {
|
||||
console.warn(`Port ${port} is already in use, trying next port...`);
|
||||
await bootstrap(port + 1);
|
||||
} else {
|
||||
console.error(`Failed to start application: ${error.message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
bootstrap(3000);
|
Reference in New Issue
Block a user