Skip to content

Commit 6dd42a5

Browse files
authored
feat(ssl): extract SSL into its own file (#295)
* 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
1 parent 095e020 commit 6dd42a5

File tree

3 files changed

+63
-69
lines changed

3 files changed

+63
-69
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"tsc": "tsc",
1010
"scully:run": "npm run scully:dev:all -- serve",
1111
"scully:run:test": "npm run test && npm run e2e",
12-
"scully:r": "node ./dist/scully/scully serve",
12+
"scully:r": "node ./dist/scully/scully serve --tds",
1313
"scully:precommit": "npm run scully:compile:all && rm -rf ./dist/static && node ./dist/scully/scully --tds && npm run test",
1414
"scully:compile:all": "ng build @scullyio/ng-lib && ng build --prod && npm run scully:dev:compile",
1515
"generate": "tsc -p ./scully/tsconfig.scully.json && node ./dist/scully/scully --tds",

scully/utils/addSSL.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {readFileSync} from 'fs';
2+
import https from 'https';
3+
import selfsigned from 'selfsigned';
4+
import {ssl, sslCert, sslKey} from '../utils/cli-options';
5+
import {log, logError, yellow} from './log';
6+
7+
export function addSSL(server, host, port) {
8+
if (!ssl) {
9+
return server;
10+
} else {
11+
let pems = {
12+
private: '',
13+
cert: '',
14+
};
15+
if (sslCert && sslKey) {
16+
try {
17+
pems.private = readFileSync(sslKey).toString();
18+
pems.cert = readFileSync(sslCert).toString();
19+
} catch (e) {
20+
logError(`Could not read the file: ${e.path}`);
21+
log(`${yellow(`Please check the path for the certificate.`)}`);
22+
process.exit(0);
23+
}
24+
} else {
25+
const attrs = [
26+
{
27+
name: 'scully',
28+
value: `${host}:${port}`,
29+
type: 'RSAPublicKey',
30+
},
31+
];
32+
pems = selfsigned.generate(attrs, {days: 365});
33+
}
34+
// serve the API with signed certificate on 443 (SSL/HTTPS) port
35+
return https.createServer(
36+
{
37+
key: pems.private,
38+
cert: pems.cert,
39+
},
40+
server
41+
);
42+
}
43+
}

scully/utils/staticServer.ts

Lines changed: 19 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
import {readFileSync} from 'fs';
1+
import express from 'express';
22
import {join} from 'path';
33
import {traverseAppRoutes} from '../routerPlugins/traverseAppRoutesPlugin';
4-
import {ssl, sslCert, sslKey, tds} from '../utils/cli-options';
4+
import {ssl, tds} from '../utils/cli-options';
5+
import {addSSL} from './addSSL';
56
import {scullyConfig} from './config';
6-
import {log, logError, yellow} from './log';
77
import {startDataServer} from './dataServer';
8+
import {log, logError, yellow} from './log';
89
import {proxyAdd} from './proxyAdd';
910

10-
const express = require('express');
11-
const https = require('https');
12-
const selfsigned = require('selfsigned');
13-
1411
let angularServerInstance: {close: () => void};
1512
let scullyServerInstance: {close: () => void};
1613
let dataServerInstance: {close: () => void};
17-
let httpsServer;
1814

1915
export async function staticServer(port?: number) {
2016
try {
2117
port = port || scullyConfig.staticport;
18+
const hostName = scullyConfig.hostName;
2219
const routes = await traverseAppRoutes();
2320
const scullyServer = express();
2421
const distFolder = join(scullyConfig.homeFolder, scullyConfig.distFolder);
@@ -44,56 +41,9 @@ export async function staticServer(port?: number) {
4441
scullyServer.use(express.static(scullyConfig.outDir, options));
4542
scullyServer.get('/', (req, res) => res.sendFile(join(distFolder, '/index.html')));
4643

47-
if (!ssl) {
48-
scullyServerInstance = scullyServer.listen(port, scullyConfig.hostName, x => {
49-
log(
50-
`Scully static server started on "${yellow(
51-
`http://${scullyConfig.hostName}:${scullyConfig.staticport}/`
52-
)}"`
53-
);
54-
});
55-
} else {
56-
let pems = {
57-
private: '',
58-
cert: '',
59-
};
60-
if (sslCert && sslKey) {
61-
try {
62-
pems.private = readFileSync(sslKey).toString();
63-
pems.cert = readFileSync(sslCert).toString();
64-
} catch (e) {
65-
logError(`Could not read the file: ${e.path}`);
66-
log(`${yellow(`Please check the path for the certificate.`)}`);
67-
process.exit(0);
68-
}
69-
} else {
70-
const attrs = [
71-
{
72-
name: 'scully',
73-
value: `${scullyConfig.hostName}:${scullyConfig.staticport}`,
74-
type: 'RSAPublicKey',
75-
},
76-
];
77-
pems = selfsigned.generate(attrs, {days: 365});
78-
console.log(pems);
79-
}
80-
// serve the API with signed certificate on 443 (SSL/HTTPS) port
81-
httpsServer = https.createServer(
82-
{
83-
key: pems.private,
84-
cert: pems.cert,
85-
},
86-
scullyServer
87-
);
88-
89-
httpsServer.listen(port, () => {
90-
log(
91-
`Scully static server started on "${yellow(
92-
`https://${scullyConfig.hostName}:${scullyConfig.staticport}/`
93-
)}"`
94-
);
95-
});
96-
}
44+
scullyServerInstance = addSSL(scullyServer, hostName, port).listen(port, hostName, x => {
45+
log(`Scully static server started on "${yellow(`http${ssl ? 's' : ''}://${hostName}:${port}/`)}"`);
46+
});
9747

9848
const angularDistServer = express();
9949
proxyAdd(angularDistServer);
@@ -119,13 +69,17 @@ export async function staticServer(port?: number) {
11969
* // angularDistServer.get('/*', (req, res) => res.sendFile(join(scullyConfig.outDir, '/index.html')));
12070
* we are already serving all known routes an index.html. at this point a 404 is indeed just a 404, don't substitute.
12171
*/
122-
angularServerInstance = angularDistServer.listen(scullyConfig.appPort, scullyConfig.hostName, x => {
123-
log(
124-
`Angular distribution server started on "${yellow(
125-
`http://${scullyConfig.hostName}:${scullyConfig.appPort}/`
126-
)}" `
127-
);
128-
});
72+
angularServerInstance = addSSL(angularDistServer, hostName, scullyConfig.appPort).listen(
73+
scullyConfig.appPort,
74+
hostName,
75+
x => {
76+
log(
77+
`Angular distribution server started on "${yellow(
78+
`http${ssl ? 's' : ''}://${hostName}:${scullyConfig.appPort}/`
79+
)}" `
80+
);
81+
}
82+
);
12983
} catch (e) {
13084
logError(`Could not start Scully serve`, e);
13185
}
@@ -138,9 +92,6 @@ export function closeExpress() {
13892
if (angularServerInstance && angularServerInstance.close) {
13993
angularServerInstance.close();
14094
}
141-
if (httpsServer) {
142-
httpsServer.close();
143-
}
14495
if (dataServerInstance && dataServerInstance.close) {
14596
dataServerInstance.close();
14697
}

0 commit comments

Comments
 (0)