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 } from 'fs' ;
9+ import { HttpsOptions } from '@nestjs/common/interfaces/external/https-options.interface' ;
10+
11+ function getHttpsOptions ( ) : HttpsOptions | null {
12+ if ( ! process . env . HTTPS_KEY_PATH || ! process . env . HTTPS_CERT_PATH ) {
13+ Logger . log ( 'HTTPS is not configured' ) ;
14+ return null ;
15+ }
16+ let cert : Buffer , key : Buffer ;
17+ try {
18+ key = readFileSync ( process . env . HTTPS_KEY_PATH ) ;
19+ } catch ( err ) {
20+ Logger . error ( 'HTTPS_KEY_PATH is not correct' , err . stack ) ;
21+ }
22+ try {
23+ cert = readFileSync ( process . env . HTTPS_CERT_PATH ) ;
24+ } catch ( err ) {
25+ Logger . error ( 'HTTPS_CERT_PATH is not correct' , err . stack ) ;
26+ }
27+ return {
28+ key,
29+ cert,
30+ } ;
31+ }
832
933async function bootstrap ( ) {
10- const app = await NestFactory . create ( AppModule , { cors : true } ) ;
34+ const app = await NestFactory . create ( AppModule , {
35+ cors : true ,
36+ httpsOptions : getHttpsOptions ( ) ,
37+ } ) ;
1138 app . useGlobalPipes ( new ValidationPipe ( ) ) ;
1239 setupSwagger ( app ) ;
1340
@@ -16,11 +43,7 @@ async function bootstrap() {
1643 }
1744
1845 // serve images
19- app . use (
20- express . static (
21- join ( process . cwd ( ) , process . env . IMG_UPLOAD_FOLDER || 'imageUploads/' )
22- )
23- ) ;
46+ app . use ( express . static ( join ( process . cwd ( ) , process . env . IMG_UPLOAD_FOLDER || 'imageUploads/' ) ) ) ;
2447
2548 await app . listen ( process . env . APP_PORT || 3000 ) ;
2649}
0 commit comments