Skip to content

Commit c649c04

Browse files
committed
https config added
1 parent 8aeeab0 commit c649c04

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

.env

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ BODY_PARSER_JSON_LIMIT="1mb"
77
APP_PORT=4200
88
APP_FRONTEND_URL=http://localhost:8080
99

10-
1110
# docker
1211
POSTGRES_PORT=5432
1312
POSTGRES_USER=postgres
1413
POSTGRES_PASSWORD=postgres
15-
POSTGRES_DB=vrt_db_dev
14+
POSTGRES_DB=vrt_db_dev
15+
16+
# optional
17+
#HTTPS_KEY_PATH='./secrets/77570959_localhost.key'
18+
#HTTPS_CERT_PATH='./secrets/77570959_localhost.cert'

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ lerna-debug.log*
3333
!.vscode/launch.json
3434
!.vscode/extensions.json
3535

36-
/imageUploads
36+
/imageUploads
37+
/secrets

src/main.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
11
import { NestFactory } from '@nestjs/core';
22
import { AppModule } from './app.module';
33
import { setupSwagger } from './swagger';
4-
import { ValidationPipe } from '@nestjs/common';
4+
import { Logger, ValidationPipe } from '@nestjs/common';
55
import * as express from 'express';
66
import { join } from 'path';
77
import * 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

933
async 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

Comments
 (0)