import { INestApplication } from '@nestjs/common' ; import { NestFactory , Reflector } from '@nestjs/core' ; import * as bodyParser from 'body-parser' ; import compression from 'compression' ; import * as rateLimit from 'express-rate-limit' ; import helmet from 'helmet' ; import { Type } from './common/shared-types' ; import { ReadOnlyRequired } from './common/types/common-types' ; import { Logger , MpcastConfig } from './config' ; import { getConfig , setConfig } from './config/config-helpers' ; import { BaseEntity } from './entity/base.entity' ; import { HttpExceptionFilter } from './filters/error.filter' ; import { ErrorInterceptor } from './interceptors/error.interceptor' ; import { LoggingInterceptor } from './interceptors/logging.interceptor' ; import { TransformInterceptor } from './interceptors/transform.interceptor' ; import { ValidationPipe } from './pipes/validation.pipe' ; export type BootstrapFunction = ( config : MpcastConfig ) => Promise < INestApplication > ; export async function bootstrap ( userConfig ) { const config = await preBootstrapConfig ( userConfig ) ; Logger . info ( `Bootstrapping Podcast Server...` ) ; const appModule = await import ( './app.module' ) ; let app ; app = await NestFactory . create ( appModule . AppModule , { cors : config . cors , logger : new Logger ( ) , } ) ; app . setGlobalPrefix ( 'api' ) ; app . use ( helmet ( ) ) ; app . use ( compression ( ) ) ; app . use ( bodyParser . json ( { limit : '1mb' } ) ) ; app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; app . useGlobalFilters ( new HttpExceptionFilter ( ) ) ; app . useGlobalPipes ( new ValidationPipe ( ) ) ; app . useGlobalInterceptors ( new TransformInterceptor ( new Reflector ( ) ) , new ErrorInterceptor ( new Reflector ( ) ) , new LoggingInterceptor ( ) ) ; await app . listen ( config . port , config . hostname ) ; logWelcomeMessage ( config ) ; return app ; } /**
 * Setting the global config must be done prior to loading the AppModule.
 */ export async function preBootstrapConfig ( userConfig ) { if ( userConfig ) { setConfig ( userConfig ) ; } const entities = await getAllEntities ( userConfig ) ; const { coreSubscribersMap } = await import ( './entity/subscribers' ) ; console . log ( userConfig ) ; setConfig ( { dbConnectionOptions : { entities , subscribers : Object . values ( coreSubscribersMap ) as any [ ] , } , } ) ; const config = getConfig ( ) ; console . log ( config ) ; Logger . useLogger ( config . logger ) ; return config ; } /**
 * Returns an array of core entities and any additional entities defined in plugins.
 */ async function getAllEntities ( userConfig ) { const { coreEntitiesMap } = await import ( './entity/entities' ) ; const coreEntities = Object . values ( coreEntitiesMap ) as any [ ] ; console . log ( coreEntities ) ; return [ ... coreEntities ] ; } function logWelcomeMessage ( config ) { let version ; try { version = require ( '../package.json' ) . version ; console . log ( version ) ; } catch ( e ) { console . log ( e ) ; version = ' unknown' ; } Logger . info ( `=================================================` ) ; Logger . info ( `Mpcast server (v${ version }) now running on port ${ config . port }` ) ; Logger . info ( `=================================================` ) ; }