added https
This commit is contained in:
31
bin/cli.js
31
bin/cli.js
@ -9,6 +9,7 @@ const path = require('path')
|
||||
const s = require('string-tools')
|
||||
const os = require('os')
|
||||
const arrayify = require('array-back')
|
||||
const t = require('typical')
|
||||
|
||||
const cli = commandLineArgs(cliOptions.definitions)
|
||||
const usage = cli.getUsage(cliOptions.usageData)
|
||||
@ -18,6 +19,8 @@ const options = collectOptions()
|
||||
if (options.misc.help) stop(usage, 0)
|
||||
if (options.misc.config) stop(JSON.stringify(options.server, null, ' '), 0)
|
||||
|
||||
validateOptions(options)
|
||||
|
||||
const app = localWebServer({
|
||||
static: {
|
||||
root: options.server.directory,
|
||||
@ -45,7 +48,22 @@ const app = localWebServer({
|
||||
mocks: options.server.mocks
|
||||
})
|
||||
|
||||
app.listen(options.server.port, onServerUp)
|
||||
let isHttps = false
|
||||
if (options.server.key && options.server.cert) {
|
||||
const https = require('https')
|
||||
const fs = require('fs')
|
||||
isHttps = true
|
||||
|
||||
const serverOptions = {
|
||||
key: fs.readFileSync(options.server.key),
|
||||
cert: fs.readFileSync(options.server.cert)
|
||||
}
|
||||
|
||||
const server = https.createServer(serverOptions, app.callback())
|
||||
server.listen(options.server.port, onServerUp)
|
||||
} else {
|
||||
app.listen(options.server.port, onServerUp)
|
||||
}
|
||||
|
||||
function stop (msgs, exitCode) {
|
||||
arrayify(msgs).forEach(msg => console.error(ansi.format(msg)))
|
||||
@ -57,7 +75,7 @@ function onServerUp () {
|
||||
.map(key => os.networkInterfaces()[key])
|
||||
.reduce((prev, curr) => prev = prev.concat(curr), [])
|
||||
.filter(iface => iface.family === 'IPv4')
|
||||
.map(iface => `[underline]{http://${iface.address}:${options.server.port}}`)
|
||||
.map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${options.server.port}}`)
|
||||
.join(', ')
|
||||
|
||||
console.error(ansi.format(
|
||||
@ -102,3 +120,12 @@ function parseRewriteRules (rules) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function validateOptions (options) {
|
||||
function invalid (msg) {
|
||||
return `[red underline]{Invalid:} [bold]{${msg}}`
|
||||
}
|
||||
if (!t.isNumber(options.server.port)) {
|
||||
stop([ invalid(`--port must be numeric [value=${options.server.port}]`), usage ], 1)
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,14 @@ module.exports = {
|
||||
description: 'Disable etag-based caching - forces loading from disk each request.', group: 'server'
|
||||
},
|
||||
{
|
||||
name: 'key', type: String, typeLabel: '[underline]{file}', group: 'server',
|
||||
description: 'SSL key. Required, along with --cert for https.'
|
||||
},
|
||||
{
|
||||
name: 'cert', type: String, typeLabel: '[underline]{file}', group: 'server',
|
||||
description: 'SSL cert. Required, along with --key for https.'
|
||||
},
|
||||
{
|
||||
name: 'verbose', type: Boolean,
|
||||
description: 'Verbose output, useful for debugging.', group: 'server'
|
||||
},
|
||||
|
Reference in New Issue
Block a user