-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ts
More file actions
51 lines (39 loc) · 1.64 KB
/
main.ts
File metadata and controls
51 lines (39 loc) · 1.64 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
import './instrument'
import { NestFactory } from '@nestjs/core'
import { ConfigService } from '@nestjs/config'
import type { NestFastifyApplication } from '@nestjs/platform-fastify'
import { FastifyAdapter } from '@nestjs/platform-fastify'
import helmet from '@fastify/helmet'
import fastifyStatic from '@fastify/static'
import { AppModule } from './app.module'
import { AppClusterService } from './cluster.service'
import { join } from 'path'
import { createAppValidationPipe } from './common/validation'
type FastifyCorsOptions = Parameters<NestFastifyApplication['enableCors']>[0]
function buildAllowedOrigins(originConfig: string): RegExp[] {
return originConfig
.split(',')
.map((s) => s.trim())
.filter(Boolean)
.map((pattern) => new RegExp('^https?://' + RegExp.escape(pattern).replace(/\\\*/g, '.*') + '$'))
}
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter())
const configService: ConfigService = app.get(ConfigService)
await app.register(fastifyStatic, {
root: join(__dirname, '..', 'public')
})
await app.register(helmet)
const allowedOrigins = buildAllowedOrigins(configService.get<string>('ALLOWED_ORIGINS', ''))
const corsOptions: FastifyCorsOptions = {
origin: (origin, callback) => {
callback(null, origin === undefined || origin === '' || allowedOrigins.some((pattern) => pattern.test(origin)))
},
credentials: true
}
app.enableCors(corsOptions)
app.useGlobalPipes(createAppValidationPipe())
await app.listen(configService.get<number>('PORT') ?? 3000, '0.0.0.0')
}
// bootstrap()
AppClusterService.clusterize(bootstrap)