forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
69 lines (58 loc) · 2.32 KB
/
Copy pathmain.ts
File metadata and controls
69 lines (58 loc) · 2.32 KB
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
64
65
66
67
68
69
import 'reflect-metadata';
import { NestFactory } from '@nestjs/core';
import { ConfigService } from '@nestjs/config';
import { ValidationPipe, VersioningType } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { AppModule } from './app.module';
import { LoggingInterceptor } from './common/interceptors/logging.interceptor';
import { InFlightRequestTracker } from './common/shutdown/in-flight-tracker.service';
import { ShutdownService } from './common/shutdown/shutdown.service';
async function bootstrap() {
const app = await NestFactory.create(AppModule, { bufferLogs: true });
app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));
app.enableVersioning({ type: VersioningType.URI });
// Issue 78 — CORS
const allowedOrigins = process.env.CORS_ORIGINS
? process.env.CORS_ORIGINS.split(',')
: ['http://localhost:3001', 'http://localhost:8081'];
app.enableCors({
origin: allowedOrigins,
credentials: true,
maxAge: 86400,
});
// Validation
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
transform: true,
forbidNonWhitelisted: true,
}),
);
// Logging
app.useGlobalInterceptors(new LoggingInterceptor());
const swaggerConfig = new DocumentBuilder()
.setTitle('Gist API')
.setDescription('Anonymous hyperlocal messaging on Stellar')
.setVersion('1.0')
.addServer('/v1', 'Version 1')
.build();
const document = SwaggerModule.createDocument(app, swaggerConfig);
SwaggerModule.setup('v1/docs', app, document);
// Issue 99 — graceful shutdown handling.
const tracker = app.get(InFlightRequestTracker);
const configService = app.get(ConfigService);
const shutdownService = new ShutdownService(app, tracker, configService);
const handle = (signal: NodeJS.Signals): void => {
void shutdownService.handleSignal(signal);
};
process.on('SIGTERM', handle);
process.on('SIGINT', handle);
await app.listen(process.env.PORT ?? 3000);
console.log(`Gist API running on port ${process.env.PORT ?? 3000}`);
console.log(`Swagger docs → http://localhost:${process.env.PORT ?? 3000}/api/docs`);
console.log(
`Graceful shutdown armed (SHUTDOWN_TIMEOUT_MS=${configService.get<number>('SHUTDOWN_TIMEOUT_MS', 25000)})`,
);
}
void bootstrap();