Skip to content

Commit

Permalink
feat(ssl): extract SSL into its own file (#295)
Browse files Browse the repository at this point in the history
* feat(ssl): extract SSL into its own file

Extract the ssl options into their own file, and add it to both servers.

* fix(cypres bug): add `--tds` to cypres test server
  • Loading branch information
SanderElias authored Feb 14, 2020
1 parent 095e020 commit 6dd42a5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 69 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 43 additions & 0 deletions scully/utils/addSSL.ts
Original file line number Diff line number Diff line change
@@ -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
);
}
}
87 changes: 19 additions & 68 deletions scully/utils/staticServer.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import {readFileSync} from 'fs';
import express from 'express';
import {join} from 'path';
import {traverseAppRoutes} from '../routerPlugins/traverseAppRoutesPlugin';
import {ssl, sslCert, sslKey, tds} from '../utils/cli-options';
import {ssl, tds} from '../utils/cli-options';
import {addSSL} from './addSSL';
import {scullyConfig} from './config';
import {log, logError, yellow} from './log';
import {startDataServer} from './dataServer';
import {log, logError, yellow} from './log';
import {proxyAdd} from './proxyAdd';

const express = require('express');
const https = require('https');
const selfsigned = require('selfsigned');

let angularServerInstance: {close: () => void};
let scullyServerInstance: {close: () => void};
let dataServerInstance: {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);
Expand All @@ -44,56 +41,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();
proxyAdd(angularDistServer);
Expand All @@ -119,13 +69,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);
}
Expand All @@ -138,9 +92,6 @@ export function closeExpress() {
if (angularServerInstance && angularServerInstance.close) {
angularServerInstance.close();
}
if (httpsServer) {
httpsServer.close();
}
if (dataServerInstance && dataServerInstance.close) {
dataServerInstance.close();
}
Expand Down

0 comments on commit 6dd42a5

Please sign in to comment.