From 0cfe724ffa089b84a9f8bca49c9033b56f51c9cb Mon Sep 17 00:00:00 2001 From: Saad Jutt Date: Mon, 14 Feb 2022 19:12:37 +0500 Subject: [PATCH] fix: release should also has https protocol --- api/.env.example | 3 +++ api/package.json | 2 -- api/src/prod-server.ts | 21 --------------------- api/src/server.ts | 42 ++++++++++++++++++++++++++++++++++++------ package.json | 2 +- 5 files changed, 40 insertions(+), 30 deletions(-) delete mode 100644 api/src/prod-server.ts diff --git a/api/.env.example b/api/.env.example index f6652e2..c808872 100644 --- a/api/.env.example +++ b/api/.env.example @@ -1,5 +1,8 @@ MODE=[desktop|server] default considered as desktop CORS=[disable|enable] default considered as disable +PROTOCOL=[http|https] default considered as http +PRIVATE_KEY=privkey.pem +FULL_CHAIN=fullchain.pem PORT=[5000] default value is 5000 PORT_WEB=[port for sasjs web component(react)] default value is 3000 ACCESS_TOKEN_SECRET= diff --git a/api/package.json b/api/package.json index e0e15e8..edf73d2 100644 --- a/api/package.json +++ b/api/package.json @@ -6,10 +6,8 @@ "scripts": { "initial": "npm run swagger && npm run compileSysInit", "prestart": "npm run initial", - "prestart:prod": "npm run initial", "prebuild": "npm run initial", "start": "nodemon ./src/server.ts", - "start:prod": "nodemon ./src/prod-server.ts", "build": "rimraf build && tsc", "swagger": "tsoa spec", "semantic-release": "semantic-release -d", diff --git a/api/src/prod-server.ts b/api/src/prod-server.ts deleted file mode 100644 index ce9d561..0000000 --- a/api/src/prod-server.ts +++ /dev/null @@ -1,21 +0,0 @@ -import path from 'path' -import { readFileSync } from 'fs' -import * as https from 'https' -import appPromise from './app' - -const keyPath = path.join('..', 'certificates', 'privkey.pem') -const certPath = path.join('..', 'certificates', 'fullchain.pem') - -const key = readFileSync(keyPath) -const cert = readFileSync(certPath) - -appPromise.then((app) => { - const httpsServer = https.createServer({ key, cert }, app) - - const sasJsPort = process.env.PORT ?? 5000 - httpsServer.listen(sasJsPort, () => { - console.log( - `⚡️[server]: Server is running at https://localhost:${sasJsPort}` - ) - }) -}) diff --git a/api/src/server.ts b/api/src/server.ts index 5ea98a8..0337162 100644 --- a/api/src/server.ts +++ b/api/src/server.ts @@ -1,10 +1,40 @@ +import path from 'path' +import { createServer } from 'https' +import { readFile } from '@sasjs/utils' + import appPromise from './app' -appPromise.then((app) => { +appPromise.then(async (app) => { + const protocol = process.env.PROTOCOL ?? 'http' const sasJsPort = process.env.PORT ?? 5000 - app.listen(sasJsPort, () => { - console.log( - `⚡️[server]: Server is running at http://localhost:${sasJsPort}` - ) - }) + + if (protocol !== 'https') { + app.listen(sasJsPort, () => { + console.log( + `⚡️[server]: Server is running at http://localhost:${sasJsPort}` + ) + }) + } else { + const { key, cert } = await getCertificates() + + const httpsServer = createServer({ key, cert }, app) + httpsServer.listen(sasJsPort, () => { + console.log( + `⚡️[server]: Server is running at https://localhost:${sasJsPort}` + ) + }) + } }) + +const getCertificates = async () => { + const privkey = process.env.PRIVATE_KEY ?? 'privkey.pem' + const fullchain = process.env.FULL_CHAIN ?? 'fullchain.pem' + + const keyPath = path.join(process.cwd(), privkey) + const certPath = path.join(process.cwd(), fullchain) + + const key = await readFile(keyPath) + const cert = await readFile(certPath) + + return { key, cert } +} diff --git a/package.json b/package.json index 7708f44..74358f0 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "server": "npm run server:prepare && npm run server:start", "server:prepare": "cd web && npm ci && npm run build && cd ../api && npm ci && cd ..", - "server:start": "cd api && npm run start:prod", + "server:start": "cd api && npm run start", "release": "standard-version", "lint-api:fix": "npx prettier --write \"api/src/**/*.{ts,tsx,js,jsx,html,css,sass,less,yml,md,graphql}\"", "lint-api": "npx prettier --check \"api/src/**/*.{ts,tsx,js,jsx,html,css,sass,less,yml,md,graphql}\"",