Skip to content

Commit d0787cd

Browse files
authored
Merge pull request #79 from Visual-Regression-Tracker/125-https-support
125 https support
2 parents 8aeeab0 + a4e63ef commit d0787cd

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-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/ssl.key'
18+
#HTTPS_CERT_PATH='./secrets/ssl.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

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
1414
- `npm run test:e2e`
1515
- Seed initial data `npx ts-node prisma/seed.ts`
1616
- `npm run start:debug`
17+
18+
## Local HTTPS config
19+
20+
- Generate keys [here](https://www.selfsignedcertificate.com/)
21+
- place in folder `/secrets` named `ssl.cert` and `ssl.key`

src/main.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
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, 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

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

Comments
 (0)