11import { NestFactory } from '@nestjs/core' ;
22import { AppModule } from './app.module' ;
33import { setupSwagger } from './swagger' ;
4- import { ValidationPipe } from '@nestjs/common' ;
4+ import { Logger , ValidationPipe } from '@nestjs/common' ;
55import * as express from 'express' ;
66import { join } from 'path' ;
77import * as bodyParser from 'body-parser' ;
8+ import { readFileSync , existsSync } from 'fs' ;
9+ import { HttpsOptions } from '@nestjs/common/interfaces/external/https-options.interface' ;
10+
11+ function getHttpsOptions ( ) : HttpsOptions | null {
12+ const keyPath = './secrets/ssl.key' ;
13+ const certPath = './secrets/ssl.cert' ;
14+ if ( ! existsSync ( keyPath ) || ! existsSync ( certPath ) ) {
15+ Logger . log ( 'HTTPS config not found. Fall back to HTTP' ) ;
16+ return null ;
17+ }
18+ return {
19+ key : readFileSync ( keyPath ) ,
20+ cert : readFileSync ( certPath ) ,
21+ } ;
22+ }
823
924async function bootstrap ( ) {
10- const app = await NestFactory . create ( AppModule , { cors : true } ) ;
25+ const app = await NestFactory . create ( AppModule , {
26+ cors : true ,
27+ httpsOptions : getHttpsOptions ( ) ,
28+ } ) ;
1129 app . useGlobalPipes ( new ValidationPipe ( ) ) ;
1230 setupSwagger ( app ) ;
1331
@@ -16,11 +34,7 @@ async function bootstrap() {
1634 }
1735
1836 // serve images
19- app . use (
20- express . static (
21- join ( process . cwd ( ) , process . env . IMG_UPLOAD_FOLDER || 'imageUploads/' )
22- )
23- ) ;
37+ app . use ( express . static ( join ( process . cwd ( ) , process . env . IMG_UPLOAD_FOLDER || 'imageUploads/' ) ) ) ;
2438
2539 await app . listen ( process . env . APP_PORT || 3000 ) ;
2640}
0 commit comments