-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
65 lines (50 loc) · 2.08 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
64
65
/**
* This is not a production server yet!
* This is only a minimal backend to get started.
*/
import { Logger, LogLevel } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app/app.module';
import { PrismaService } from "./prisma/prisma.service";
import { intersection } from 'lodash';
import { SwaggerModule } from "@nestjs/swagger";
import { getSwaggerDocumentationConfig } from "./swagger/SwaggerDocumentConfig";
import { PrismaClientExceptionFilter } from "./exception-filters/PrismaClientExceptionFilter";
const logger = new Logger('bootstrap', { timestamp: true });
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: getLogLevelsFromEnv()
});
const globalPrefix = 'api';
app.setGlobalPrefix(globalPrefix);
app.useGlobalFilters(new PrismaClientExceptionFilter(app.getHttpAdapter()));
const document = SwaggerModule.createDocument(app, getSwaggerDocumentationConfig());
SwaggerModule.setup('swagger', app, document, {
customSiteTitle: 'Swagger documentation for Energy Web Zero PL POC API',
swaggerOptions: {
persistAuthorization: true,
},
});
app.enableCors({
origin: process.env.CORS_ORIGIN,
maxAge: parseInt(process.env.CORS_MAX_AGE)
});
const port = process.env.PORT || 3333;
await app.listen(port, () => {
Logger.log('Listening at http://localhost:' + port + '/' + globalPrefix);
});
const prisma = app.get<PrismaService>(PrismaService);
prisma.$on('beforeExit', async function () {
logger.log('Prisma client "beforeExit" event, initiating shut down routine');
// hacky way of enabling shutdown hooks because prisma handles stop signals itself
logger.debug('awaiting application closed');
await app.close();
logger.debug('application closed');
});
}
bootstrap();
function getLogLevelsFromEnv(): LogLevel[] {
const allowedLogLevels: LogLevel[] = ['log', 'error', 'warn', 'debug', 'verbose'];
const envLogLevels = (process.env.LOG_LEVELS).split(',') as LogLevel[];
return intersection(allowedLogLevels, envLogLevels) as LogLevel[]
}