Add support for running in ssl mode

Also added helpful argument to print current config and exit
This commit is contained in:
Jonatan Nilsson 2021-10-14 09:57:08 +00:00
parent f8b0dbc943
commit 33af1798b3
6 changed files with 77 additions and 8 deletions

10
bin.mjs
View file

@ -12,6 +12,11 @@ if (!config.get('file') && !config.get(env + ':file') &&
displayHelp = true; displayHelp = true;
} }
if (config.get('config')) {
console.log(config.get())
process.exit(0)
}
if (displayHelp) { if (displayHelp) {
console.log(`Run static server for static files, simple servers or pure MVVM projects. console.log(`Run static server for static files, simple servers or pure MVVM projects.
Specifying either file or folder serving is required. Specifying either file or folder serving is required.
@ -37,6 +42,11 @@ Options:
--ip IP server runs on [default: 0.0.0.0] --ip IP server runs on [default: 0.0.0.0]
--rooturlpath Root URL path server is deployed on; will be removed from --rooturlpath Root URL path server is deployed on; will be removed from
URL when resolving to files [default: /] URL when resolving to files [default: /]
--ssl Specify whether to serve in ssl mode. Uses the path of
--sslkey and --sslcert.
--sslkey The path to the ssl key for ssl mode [default: key.pem]
--sslcert The path to the ssl certificate for ssl mode [default:
cert.pem]
Examples: Examples:
spserver -p 2000 -f base.html -s ./dist spserver -p 2000 -f base.html -s ./dist

19
cert.pem Normal file
View file

@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDFzCCAf8CFAVgLggEDpAeBShzYOqTu8/OA+LHMA0GCSqGSIb3DQEBCwUAMEgx
CzAJBgNVBAYTAlVTMRMwEQYDVQQIDApTb21lLVN0YXRlMREwDwYDVQQKDAhzcHNl
cnZlcjERMA8GA1UEAwwIc3BzZXJ2ZXIwHhcNMjExMDE0MDk0MDA2WhcNNDkwMjI4
MDk0MDA2WjBIMQswCQYDVQQGEwJVUzETMBEGA1UECAwKU29tZS1TdGF0ZTERMA8G
A1UECgwIc3BzZXJ2ZXIxETAPBgNVBAMMCHNwc2VydmVyMIIBIjANBgkqhkiG9w0B
AQEFAAOCAQ8AMIIBCgKCAQEAq7r5IgsLAEBg4cxJSHkKk+FXTyXHWwt3XBKheGOo
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xipubWRBSs0Wvznyg+d/iYvSxbooAayUcN8PzXYpo+KWDUakK4As7HADDYrG+Fm
zJh4LwKBgQCEZVroxl14cVBEAtY7rWP1OaLvvDXJNhWMtdX1BawHXAFOigQJB2N4
0HA6dOJ0SBmPF8WlJUYM2aRy050T+Qiu9fIl1vlNKW0zOBj+lPQo7T4JTu/WPpbr
ZKVf12+b/RbV1ch3mb4KxkX80ASIfVaMRGQHAAgDpCM8fMVmm4NjdQ==
-----END RSA PRIVATE KEY-----

View file

@ -40,6 +40,8 @@ nconf.defaults({
level: 'info', level: 'info',
}, },
}, },
sslkey: 'key.pem',
sslcert: 'cert.pem',
development: { development: {
port: 3001, port: 3001,
bunyan: { bunyan: {

View file

@ -1,5 +1,6 @@
import fs from 'fs' import fs from 'fs'
import http from 'http' import http from 'http'
import https from 'https'
import path from 'path' import path from 'path'
import url from 'url' import url from 'url'
import _ from 'lodash' import _ from 'lodash'
@ -71,7 +72,7 @@ class SPServer {
var base = SPServer.generateBase(finalSettings.file ? path.resolve(finalSettings.file) : null, finalSettings); var base = SPServer.generateBase(finalSettings.file ? path.resolve(finalSettings.file) : null, finalSettings);
var server = http.createServer(function (req, res) { let requestHandler = function (req, res) {
var startTime = new Date().getTime(); var startTime = new Date().getTime();
var isFinished = false var isFinished = false
@ -106,15 +107,25 @@ class SPServer {
} }
}); });
}).resume(); }).resume();
}); }
var server
if (config.get('ssl')) {
server = https.createServer({
key: fs.readFileSync(config.get('sslkey')),
cert: fs.readFileSync(config.get('sslcert'))
}, requestHandler)
} else {
server = http.createServer(requestHandler)
}
server.listen(finalSettings.port, finalSettings.ip); server.listen(finalSettings.port, finalSettings.ip);
logger.info( logger.info('Started single-page server: ' + finalSettings.name
'Started single-page server: ' + finalSettings.name + + ', base file: ' + (finalSettings.file || '<none>')
', base file: ' + (finalSettings.file || '<none>') + + ', static folder: ' + finalSettings.serve
', static folder: ' + finalSettings.serve + + ', port: ' + finalSettings.port
', port: ' + finalSettings.port + (config.get('ssl') ? ' [SSL]' : '')
); );
this.server = server this.server = server

View file

@ -24,7 +24,7 @@
"dependencies": { "dependencies": {
"bunyan-lite": "^1.1.1", "bunyan-lite": "^1.1.1",
"lodash": "^4.17.2", "lodash": "^4.17.2",
"nconf-lite": "^2.0.0", "nconf-lite": "^2.1.0",
"node-static": "^0.7.6" "node-static": "^0.7.6"
}, },
"bin": "./bin.mjs", "bin": "./bin.mjs",