/
main.ts
63 lines (56 loc) · 1.59 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Body, Controller, Module, Post } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { ApiProperty, ApiResponse, DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { IsString, IsNumber, Min, Max, IsNotEmpty } from 'class-validator';
import { Logger } from 'gc-json-logger';
import { Trace, TracingModule } from '../';
class Student {
@ApiProperty({ type: String })
@IsNotEmpty()
@IsString()
firstName!: string;
@ApiProperty({ type: String })
@IsNotEmpty()
@IsString()
lastName!: string;
@ApiProperty({ type: Number })
@Min(18)
@Max(100)
@IsNumber()
age!: number;
}
@Controller()
export class AppController {
@Post('/students')
@Trace() // 1) adds headers definition for your OpenAPI
@ApiResponse({ type: String })
registerPerson(@Body() newPerson: Student) {
// 2) log something
Logger.info('Registering', { newPerson });
return { registed: true };
}
}
@Module({
imports: [
TracingModule.register({
// 3) specify which routes to trace
routes: ['*'],
onRequest(uuid, next) {
Logger.runWith(new Logger(uuid), () => {
next();
});
},
}),
],
controllers: [AppController],
})
export class AppModule {}
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
const config = new DocumentBuilder().build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('/', app, document);
await app.listen(3000);
}
bootstrap();