universo-platform-3d
115 строк · 4.1 Кб
1require('dotenv').config()
2import { ValidationPipe } from '@nestjs/common'
3import { NestFactory } from '@nestjs/core'
4import { NestExpressApplication } from '@nestjs/platform-express'
5import { WsAdapter } from '@nestjs/platform-ws'
6import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'
7import { SentryService } from '@ntegral/nestjs-sentry'
8import {
9WinstonModule,
10utilities as nestWinstonModuleUtilities
11} from 'nest-winston'
12import * as os from 'os'
13import * as winston from 'winston'
14import { AppModule } from './app.module'
15import AllExceptionsFilter from './error-handling/all-exceptions.filter'
16import metadata from './metadata'
17import { NODE_ENV } from './util/node-env.enum'
18
19async function bootstrapMirrorWebServer() {
20console.log('Starting mirror-web-server')
21
22const version = require('../package.json').version // eslint-disable-line @typescript-eslint/no-var-requires
23const app = await NestFactory.create<NestExpressApplication>(AppModule, {
24// 2023-07-13 23:04:10: with Winston, there's weirdness with the test setup where the logs get suppressed in tests. This obviously isnt desirable when running tests, so I've added this ternary to not use Winston in tests.
25logger:
26process.env.NODE_ENV === NODE_ENV.TEST
27? undefined
28: WinstonModule.createLogger({
29transports: [
30new winston.transports.Console({
31format: winston.format.combine(
32winston.format.timestamp(),
33winston.format.ms(),
34nestWinstonModuleUtilities.format.nestLike('mirror-server', {
35colors: true,
36prettyPrint: true
37})
38),
39level: process.env.NODE_ENV === NODE_ENV.TEST ? 'debug' : 'info'
40})
41// other transports...
42]
43// other options
44}),
45rawBody: true //**This is required for stripe webhook
46})
47
48/**
49* Swagger
50*/
51await SwaggerModule.loadPluginMetadata(metadata)
52const config = new DocumentBuilder()
53.setTitle('Mirror Web Server')
54.setDescription('Mirror Web Server API')
55.setVersion(version)
56.build()
57const document = SwaggerModule.createDocument(app, config)
58// For the publicly-exposed docs, append random string of characters appended to "hide" it
59SwaggerModule.setup(
60'api-bejmnvpnugdasdfjkasdjfkasjdfjksdfasdui9823hui23',
61app,
62document
63)
64// If on localhost in dev, then still allow for http://localhost:9000/api
65if (
66os.hostname().includes('local') &&
67process.env.NODE_ENV !== 'production' &&
68!process.env.K_REVISION // This is truthy if on GCP cloud run
69) {
70SwaggerModule.setup('api', app, document)
71}
72
73// default
74app.enableCors({
75// cors has to be here
76origin: '*' // TODO change to only the frontend
77})
78app.useWebSocketAdapter(new WsAdapter(app))
79
80// asset storage driver env validation
81if (
82process.env.ASSET_STORAGE_DRIVER === 'GCP' &&
83!process.env.GCS_BUCKET_PUBLIC
84) {
85throw new Error('GCS_BUCKET_PUBLIC is required when using GCP storage')
86}
87
88if (
89(!process.env.ASSET_STORAGE_DRIVER ||
90process.env.ASSET_STORAGE_DRIVER === 'LOCAL') &&
91!process.env.ASSET_STORAGE_URL
92) {
93throw new Error('ASSET_STORAGE_URL is required when using LOCAL storage')
94}
95
96// Important: This *only* sets up validation pipes for HTTP handlers, NOT Websockets. See the notice here: https://docs.nestjs.com/pipes#global-scoped-pipes
97app.useGlobalPipes(
98new ValidationPipe({
99transform: true,
100// whitelist: true, // Do NOT remove whitelist: true. This is a big security risk if it's not there since we pass dtos from the controllers directly into Mongo
101enableDebugMessages:
102process.env.NODE_ENV === NODE_ENV.DEVELOPMENT ? true : false
103})
104)
105
106// Add global exception filter for Sentry to handle 500 errors
107const sentryService = app.get<SentryService>(SentryService)
108app.useGlobalFilters(new AllExceptionsFilter(sentryService))
109
110const port = process.env.PORT || 9000
111console.log(`Running on port ${port}`)
112await app.listen(port)
113}
114
115bootstrapMirrorWebServer()
116