Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTPS option #19

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions src/config/ducky-api-config.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,14 @@ export class DuckyApiConfig {
@IsNumber()
@Min(0)
DELAY: number

@IsOptional()
@IsNotEmpty()
@IsString()
TLS_KEY_PATH: string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TLS_KEY_PATH: string
TLS_KEY_PATH?: string


@IsOptional()
@IsNotEmpty()
@IsString()
TLS_CERT_PATH: string
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TLS_CERT_PATH: string
TLS_CERT_PATH?: string

}
24 changes: 22 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import fs from 'fs'
import Helmet from 'helmet'
import { resolve } from 'path'
import { promisify } from 'util'
import { ExpressAdapter } from '@nestjs/platform-express'
import * as express from 'express';

import { AppModule } from './app.module'
import { UnauthorizedExceptionFilter } from './common/filters/unauthorized-exception.filter'
Expand All @@ -15,7 +17,12 @@ const writeFile = promisify(fs.writeFile)
declare const module: any

async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule)
const server = express.default()
const app = await NestFactory.create(
AppModule,
new ExpressAdapter(server)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason to do it this way I can see from the documention if for starting multiple servers at the same time (for example an http server next to an https server), which is not something that's happening right now. Is there a reason you did it this way as opposed to the standard nestjs config?

)

const config: ConfigService = app.get('ConfigService')

if (config.SERVE_DUCKYPANEL) {
Expand Down Expand Up @@ -55,8 +62,21 @@ async function bootstrap(): Promise<void> {
displayOperationId: true,
},
})

await app.init()

await app.listen(config.PORT)
if (config.TLS_KEY_PATH && config.TLS_CERT_PATH) {
const https = require('https')
const fs = require('fs')
Comment on lines +69 to +70
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make these import statements at the top of the file instead of requires.

await https.createServer({
key: fs.readFileSync(config.TLS_KEY_PATH),
cert: fs.readFileSync(config.TLS_CERT_PATH)
}, server).listen(config.PORT)
}
else {
const http = require('http')
await http.createServer(server).listen(config.PORT)
}

if (module.hot) {
module.hot.accept()
Expand Down