api: приложение NestJS и e2e-тесты
Каркас api: NestJS со сборкой через webpack, health-контроллер с версией и статусом БД, Dockerfile, тесты Vitest и e2e-тесты на Jest.
This commit is contained in:
1 parent
94e5e09218
commit
94f9b06527
27 files changed
+791
No files matched your search
@@ -0,0 +1,12 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor(private readonly appService: AppService) {}
|
||||
|
||||
@Get()
|
||||
getData() {
|
||||
return this.appService.getData();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
controllers: [AppController],
|
||||
providers: [AppService],
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getData(): { message: string } {
|
||||
return { message: 'Hello API' };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { HealthCheck, HealthCheckService, TypeOrmHealthIndicator } from '@nestjs/terminus';
|
||||
|
||||
declare const __APP_VERSION__: string;
|
||||
|
||||
@ApiTags('Health Check')
|
||||
@Controller('health')
|
||||
export class HealthController {
|
||||
constructor(
|
||||
private health: HealthCheckService,
|
||||
private db: TypeOrmHealthIndicator,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
@HealthCheck()
|
||||
@ApiOperation({ summary: 'Проверка состояния приложения' })
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'Приложение работает нормально',
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
status: { type: 'string', example: 'ok' },
|
||||
version: { type: 'string', example: 'v1.2.3' },
|
||||
info: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
database: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
status: { type: 'string', example: 'up' },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
async check() {
|
||||
const result = await this.health.check([() => this.db.pingCheck('database')]);
|
||||
return {
|
||||
...result,
|
||||
version: __APP_VERSION__,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
|
||||
import { AppModule } from './app/app.module';
|
||||
|
||||
async function start() {
|
||||
const PORT = process.env.PORT || 5000;
|
||||
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
// настройка swaggera
|
||||
const config = new DocumentBuilder()
|
||||
.setTitle('GEO Helm Backend')
|
||||
.setDescription('Documentation for the GEO Helm Backend')
|
||||
.setVersion('1.0.0')
|
||||
.addBearerAuth(
|
||||
{
|
||||
type: 'http',
|
||||
scheme: 'bearer',
|
||||
bearerFormat: 'JWT',
|
||||
name: 'JWT',
|
||||
description: 'Enter JWT token',
|
||||
in: 'header',
|
||||
},
|
||||
'JWT-auth',
|
||||
)
|
||||
.build();
|
||||
app.enableShutdownHooks();
|
||||
app.setGlobalPrefix('api');
|
||||
const server = app.getHttpServer();
|
||||
server.setTimeout(20 * 60 * 1000); // 20-minute window for heavy calculations
|
||||
// подключаем swagger к приложению и указываем путь к документации
|
||||
const document = SwaggerModule.createDocument(app, config);
|
||||
SwaggerModule.setup('/api/docs', app, document);
|
||||
app.enableCors({
|
||||
origin:
|
||||
process.env.NODE_ENV === 'development'
|
||||
? ['http://localhost:5173']
|
||||
: ['https://app.igeo.pro'],
|
||||
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
|
||||
allowedHeaders: 'Content-Type, Authorization',
|
||||
credentials: true,
|
||||
});
|
||||
await app.listen(PORT, () => console.log(`Server started on ${PORT}`));
|
||||
}
|
||||
start();
|
||||
@@ -0,0 +1 @@
|
||||
import 'reflect-metadata';
|
||||
Reference in new issue
Block a user