Files
hiring-test-one/lib/local-web-server.js

131 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-06-08 23:06:41 +01:00
#!/usr/bin/env node
2015-11-08 22:09:07 +00:00
'use strict'
2016-06-08 23:06:41 +01:00
const ansi = require('ansi-escape-sequences')
2015-11-13 11:26:02 +00:00
const path = require('path')
2015-11-20 13:50:49 +00:00
const arrayify = require('array-back')
2016-06-08 23:06:41 +01:00
const t = require('typical')
const Tool = require('command-line-tool')
2016-06-16 23:00:07 +01:00
const MiddlewareStack = require('./middleware-stack')
2016-06-08 23:06:41 +01:00
const tool = new Tool()
2015-11-08 22:09:07 +00:00
2016-06-16 23:00:07 +01:00
class Cli extends MiddlewareStack {
start () {
const options = collectOptions(this.getOptionDefinitions())
this.options = options
2015-11-08 22:09:07 +00:00
2016-06-16 23:00:07 +01:00
if (options.misc.verbose) {
process.env.DEBUG = '*'
}
2015-11-08 22:09:07 +00:00
2016-06-08 23:06:41 +01:00
if (options.misc.config) {
2016-06-16 23:00:07 +01:00
tool.stop(JSON.stringify(options, null, ' '), 0)
2016-06-08 23:06:41 +01:00
} else {
const Koa = require('koa')
const app = new Koa()
app.on('error', err => {
2016-06-16 23:00:07 +01:00
if (options.middleware['log-format']) {
2016-06-08 23:06:41 +01:00
console.error(ansi.format(err.message, 'red'))
}
})
2016-06-15 21:02:52 +01:00
2016-06-16 23:00:07 +01:00
app.use(this.compose(options))
2015-11-18 16:37:16 +00:00
2016-06-16 23:00:07 +01:00
let key = options.server.key
let cert = options.server.cert
if (options.server.https) {
key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
}
2015-11-20 13:50:49 +00:00
2016-06-16 23:00:07 +01:00
if (key && cert) {
const https = require('https')
const fs = require('fs')
2015-11-20 13:50:49 +00:00
2016-06-16 23:00:07 +01:00
const serverOptions = {
key: fs.readFileSync(key),
cert: fs.readFileSync(cert)
}
2016-06-16 23:00:07 +01:00
const server = https.createServer(serverOptions, app.callback())
server.listen(options.server.port, onServerUp.bind(null, options, true))
} else {
app.listen(options.server.port, onServerUp.bind(null, options))
}
2016-06-08 23:06:41 +01:00
}
2015-11-13 19:55:52 +00:00
}
2016-06-08 23:06:41 +01:00
}
2015-11-13 15:33:19 +00:00
2016-06-08 23:06:41 +01:00
function onServerUp (options, isHttps) {
const ipList = getIPList(isHttps)
.map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${options.server.port}}`)
.join(', ')
2015-11-13 15:33:19 +00:00
2016-06-08 23:06:41 +01:00
console.error(ansi.format(
2016-06-16 23:00:07 +01:00
path.resolve(options.middleware.directory) === process.cwd()
2016-06-08 23:06:41 +01:00
? `serving at ${ipList}`
2016-06-16 23:00:07 +01:00
: `serving [underline]{${options.middleware.directory}} at ${ipList}`
2016-06-08 23:06:41 +01:00
))
}
2016-06-08 23:06:41 +01:00
function getIPList (isHttps) {
const flatten = require('reduce-flatten')
const os = require('os')
2015-11-12 23:02:38 +00:00
2016-06-08 23:06:41 +01:00
let ipList = Object.keys(os.networkInterfaces())
.map(key => os.networkInterfaces()[key])
.reduce(flatten, [])
.filter(iface => iface.family === 'IPv4')
ipList.unshift({ address: os.hostname() })
return ipList
}
2015-11-12 23:02:38 +00:00
2016-06-08 23:06:41 +01:00
/**
* Return default, stored and command-line options combined
*/
2016-06-16 23:00:07 +01:00
function collectOptions (mwOptionDefinitions) {
2016-06-08 23:06:41 +01:00
const loadConfig = require('config-master')
const stored = loadConfig('local-web-server')
const cli = require('../lib/cli-data')
2015-11-24 11:46:47 +00:00
2016-06-08 23:06:41 +01:00
/* parse command line args */
2016-06-16 23:00:07 +01:00
const definitions = cli.optionDefinitions.concat(arrayify(mwOptionDefinitions))
let options = tool.getOptions(definitions, cli.usage(definitions))
2015-11-18 16:37:16 +00:00
2016-06-16 23:00:07 +01:00
/* override built-in defaults with stored config and then command line args */
options.server = Object.assign({ port: 8000 }, stored.server, options.server)
options.middleware = Object.assign({ directory: process.cwd() }, stored.middleware || {}, options.middleware)
2016-06-16 23:00:07 +01:00
if (options.middleware.rewrite) {
options.middleware.rewrite = parseRewriteRules(options.middleware.rewrite)
2015-11-10 21:50:56 +00:00
}
2015-11-13 15:33:19 +00:00
2016-06-08 23:06:41 +01:00
validateOptions(options)
2016-06-16 23:00:07 +01:00
// console.log(options)
2016-06-08 23:06:41 +01:00
return options
2015-11-08 22:09:07 +00:00
}
2016-06-08 23:06:41 +01:00
function parseRewriteRules (rules) {
return rules && rules.map(rule => {
2016-06-16 23:00:07 +01:00
if (t.isString(rule)) {
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
return {
from: matches[1],
to: matches[2]
}
} else {
return rule
2016-06-08 23:06:41 +01:00
}
})
}
2015-11-13 15:33:19 +00:00
2016-06-08 23:06:41 +01:00
function validateOptions (options) {
if (!t.isNumber(options.server.port)) {
tool.printError('--port must be numeric')
console.error(tool.usage)
tool.halt()
}
}
2015-11-19 10:17:02 +00:00
2016-06-08 23:06:41 +01:00
module.exports = Cli