-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain.ts
101 lines (85 loc) · 2.97 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import session from 'express-session';
import { NestFactory } from '@nestjs/core';
import { ConfigService } from '@nestjs/config';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { json, urlencoded } from 'body-parser';
import { useContainer } from 'class-validator';
import helmet from 'helmet';
import cookieParser from 'cookie-parser';
import { AppModule } from './app.module';
import { ConfigNames } from './common/config';
import { createWriteStream } from 'fs';
import { get } from 'http';
import { INestApplication } from '@nestjs/common';
async function bootstrap() {
const app = await NestFactory.create<INestApplication>(AppModule, {
cors: true,
});
const configService: ConfigService = app.get(ConfigService);
const baseUrl = configService.get<string>(ConfigNames.FE_URL)!;
app.enableCors({
origin:
process.env.NODE_ENV === 'development' ||
process.env.NODE_ENV === 'staging'
? [
`http://localhost:3001`,
`http://127.0.0.1:3001`,
`http://0.0.0.0:3001`,
baseUrl,
]
: [baseUrl],
credentials: true,
exposedHeaders: ['Content-Disposition'],
});
useContainer(app.select(AppModule), { fallbackOnErrors: true });
app.use(cookieParser());
const sessionSecret = configService.get<string>(ConfigNames.SESSION_SECRET)!;
app.use(
session({
secret: sessionSecret,
resave: false,
saveUninitialized: false,
}),
);
app.use(json({ limit: '5mb' }));
app.use(urlencoded({ limit: '5mb', extended: true }));
const config = new DocumentBuilder()
.addBearerAuth()
.setTitle('Job Launcher API')
.setDescription('Swagger Job Launcher API')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('swagger', app, document);
const host = configService.get<string>(ConfigNames.HOST)!;
const port = configService.get<string>(ConfigNames.PORT)!;
app.use(helmet());
await app.listen(port, host, async () => {
console.info(`API server is running on http://${host}:${port}`);
});
//Download files to serve them as static files when the app is deployed on vercel
if (process.env.NODE_ENV === 'development') {
// write swagger ui files
get(
`http://${host}:${port}/swagger/swagger-ui-bundle.js`,
function (response: { pipe: (arg0: any) => void }) {
response.pipe(createWriteStream('swagger-static/swagger-ui-bundle.js'));
},
);
get(
`http://${host}:${port}/swagger/swagger-ui-standalone-preset.js`,
function (response: { pipe: (arg0: any) => void }) {
response.pipe(
createWriteStream('swagger-static/swagger-ui-standalone-preset.js'),
);
},
);
get(
`http://${host}:${port}/swagger/swagger-ui.css`,
function (response: { pipe: (arg0: any) => void }) {
response.pipe(createWriteStream('swagger-static/swagger-ui.css'));
},
);
}
}
void bootstrap();