From f06b8e8a897a25976c3f9ee96ad8e3cea1109381 Mon Sep 17 00:00:00 2001 From: Sander Elias Date: Fri, 14 Feb 2020 11:48:15 +0100 Subject: [PATCH 1/2] feat(ssl): extract SSL into its own file Extract the ssl options into their own file, and add it to both servers. --- scully/utils/addSSL.ts | 43 ++++++++++++++++++ scully/utils/staticServer.ts | 87 ++++++++---------------------------- 2 files changed, 62 insertions(+), 68 deletions(-) create mode 100644 scully/utils/addSSL.ts diff --git a/scully/utils/addSSL.ts b/scully/utils/addSSL.ts new file mode 100644 index 000000000..8f0ff8b8d --- /dev/null +++ b/scully/utils/addSSL.ts @@ -0,0 +1,43 @@ +import {readFileSync} from 'fs'; +import https from 'https'; +import selfsigned from 'selfsigned'; +import {ssl, sslCert, sslKey} from '../utils/cli-options'; +import {log, logError, yellow} from './log'; + +export function addSSL(server, host, port) { + if (!ssl) { + return server; + } else { + let pems = { + private: '', + cert: '', + }; + if (sslCert && sslKey) { + try { + pems.private = readFileSync(sslKey).toString(); + pems.cert = readFileSync(sslCert).toString(); + } catch (e) { + logError(`Could not read the file: ${e.path}`); + log(`${yellow(`Please check the path for the certificate.`)}`); + process.exit(0); + } + } else { + const attrs = [ + { + name: 'scully', + value: `${host}:${port}`, + type: 'RSAPublicKey', + }, + ]; + pems = selfsigned.generate(attrs, {days: 365}); + } + // serve the API with signed certificate on 443 (SSL/HTTPS) port + return https.createServer( + { + key: pems.private, + cert: pems.cert, + }, + server + ); + } +} diff --git a/scully/utils/staticServer.ts b/scully/utils/staticServer.ts index c1980daf1..d6dfefc2b 100644 --- a/scully/utils/staticServer.ts +++ b/scully/utils/staticServer.ts @@ -1,21 +1,18 @@ +import express from 'express'; import {join} from 'path'; import {traverseAppRoutes} from '../routerPlugins/traverseAppRoutesPlugin'; +import {ssl} from '../utils/cli-options'; +import {addSSL} from './addSSL'; import {scullyConfig} from './config'; -import {log, logError, red, yellow} from './log'; -import {ssl, sslCert, sslKey} from '../utils/cli-options'; -import {readFileSync} from 'fs'; - -const express = require('express'); -const https = require('https'); -const selfsigned = require('selfsigned'); +import {log, logError, yellow} from './log'; let angularServerInstance: {close: () => void}; let scullyServerInstance: {close: () => void}; -let httpsServer; export async function staticServer(port?: number) { try { port = port || scullyConfig.staticport; + const hostName = scullyConfig.hostName; const routes = await traverseAppRoutes(); const scullyServer = express(); const distFolder = join(scullyConfig.homeFolder, scullyConfig.distFolder); @@ -35,56 +32,9 @@ export async function staticServer(port?: number) { scullyServer.use(express.static(scullyConfig.outDir, options)); scullyServer.get('/', (req, res) => res.sendFile(join(distFolder, '/index.html'))); - if (!ssl) { - scullyServerInstance = scullyServer.listen(port, scullyConfig.hostName, x => { - log( - `Scully static server started on "${yellow( - `http://${scullyConfig.hostName}:${scullyConfig.staticport}/` - )}"` - ); - }); - } else { - let pems = { - private: '', - cert: '', - }; - if (sslCert && sslKey) { - try { - pems.private = readFileSync(sslKey).toString(); - pems.cert = readFileSync(sslCert).toString(); - } catch (e) { - logError(`Could not read the file: ${e.path}`); - log(`${yellow(`Please check the path for the certificate.`)}`); - process.exit(0); - } - } else { - const attrs = [ - { - name: 'scully', - value: `${scullyConfig.hostName}:${scullyConfig.staticport}`, - type: 'RSAPublicKey', - }, - ]; - pems = selfsigned.generate(attrs, {days: 365}); - console.log(pems); - } - // serve the API with signed certificate on 443 (SSL/HTTPS) port - httpsServer = https.createServer( - { - key: pems.private, - cert: pems.cert, - }, - scullyServer - ); - - httpsServer.listen(port, () => { - log( - `Scully static server started on "${yellow( - `https://${scullyConfig.hostName}:${scullyConfig.staticport}/` - )}"` - ); - }); - } + scullyServerInstance = addSSL(scullyServer, hostName, port).listen(port, hostName, x => { + log(`Scully static server started on "${yellow(`http${ssl ? 's' : ''}://${hostName}:${port}/`)}"`); + }); const angularDistServer = express(); angularDistServer.get('/_pong', (req, res) => { @@ -109,13 +59,17 @@ export async function staticServer(port?: number) { * // angularDistServer.get('/*', (req, res) => res.sendFile(join(scullyConfig.outDir, '/index.html'))); * we are already serving all known routes an index.html. at this point a 404 is indeed just a 404, don't substitute. */ - angularServerInstance = angularDistServer.listen(scullyConfig.appPort, scullyConfig.hostName, x => { - log( - `Angular distribution server started on "${yellow( - `http://${scullyConfig.hostName}:${scullyConfig.appPort}/` - )}" ` - ); - }); + angularServerInstance = addSSL(angularDistServer, hostName, scullyConfig.appPort).listen( + scullyConfig.appPort, + hostName, + x => { + log( + `Angular distribution server started on "${yellow( + `http${ssl ? 's' : ''}://${hostName}:${scullyConfig.appPort}/` + )}" ` + ); + } + ); } catch (e) { logError(`Could not start Scully serve`, e); } @@ -128,7 +82,4 @@ export function closeExpress() { if (angularServerInstance && angularServerInstance.close) { angularServerInstance.close(); } - if (httpsServer) { - httpsServer.close(); - } } From 0f114afeba8255d582c91af227ee7f1d56a8c7d9 Mon Sep 17 00:00:00 2001 From: Sander Elias Date: Fri, 14 Feb 2020 13:15:29 +0100 Subject: [PATCH 2/2] fix(cypres bug): add `--tds` to cypres test server --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f9da3b833..bb6aa40fd 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "tsc": "tsc", "scully:run": "npm run scully:dev:all -- serve", "scully:run:test": "npm run test && npm run e2e", - "scully:r": "node ./dist/scully/scully serve", + "scully:r": "node ./dist/scully/scully serve --tds", "scully:precommit": "npm run scully:compile:all && rm -rf ./dist/static && node ./dist/scully/scully --tds && npm run test", "scully:compile:all": "ng build @scullyio/ng-lib && ng build --prod && npm run scully:dev:compile", "generate": "tsc -p ./scully/tsconfig.scully.json && node ./dist/scully/scully --tds",