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

132 lines
3.4 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')
const tool = new Tool()
2015-11-08 22:09:07 +00:00
2016-06-08 23:06:41 +01:00
class Cli {
2016-06-15 21:02:52 +01:00
constructor () {
this.options = collectOptions()
2016-06-08 23:06:41 +01:00
this.app = null
this.middleware = null
2015-11-08 22:09:07 +00:00
2016-06-15 21:02:52 +01:00
const options = this.options
2015-11-08 22:09:07 +00:00
2016-06-08 23:06:41 +01:00
if (options.misc.config) {
tool.stop(JSON.stringify(options.server, null, ' '), 0)
} else {
const Koa = require('koa')
const app = new Koa()
app.on('error', err => {
if (options.server['log-format']) {
console.error(ansi.format(err.message, 'red'))
}
})
2016-06-15 21:02:52 +01:00
this.app = app
const MiddlewareStack = require('./middleware-stack')
this.middleware = new MiddlewareStack(options)
2016-06-08 23:06:41 +01:00
}
2015-11-18 16:37:16 +00:00
}
2016-06-08 23:06:41 +01:00
listen () {
2016-06-15 21:02:52 +01:00
this.app.use(this.middleware.compose())
2016-06-08 23:06:41 +01:00
const options = this.options
2016-06-15 21:02:52 +01:00
const key = this.options.server.key
const cert = this.options.server.cert
2016-06-08 23:06:41 +01:00
if (options.server.https) {
2016-06-15 21:02:52 +01:00
key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
2016-06-08 23:06:41 +01:00
}
2015-11-20 13:50:49 +00:00
2016-06-15 21:02:52 +01:00
if (key && cert) {
2016-06-08 23:06:41 +01:00
const https = require('https')
const fs = require('fs')
2015-11-20 13:50:49 +00:00
2016-06-08 23:06:41 +01:00
const serverOptions = {
2016-06-15 21:02:52 +01:00
key: fs.readFileSync(key),
cert: fs.readFileSync(cert)
2015-11-16 20:47:34 +00:00
}
2016-06-08 23:06:41 +01:00
const server = https.createServer(serverOptions, this.app.callback())
server.listen(options.server.port, onServerUp.bind(null, options, true))
} else {
this.app.listen(options.server.port, onServerUp.bind(null, options))
}
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(
path.resolve(options.server.directory) === process.cwd()
? `serving at ${ipList}`
: `serving [underline]{${options.server.directory}} at ${ipList}`
))
}
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
*/
function collectOptions () {
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 */
let options = tool.getOptions(cli.optionDefinitions, cli.usage)
2015-11-18 16:37:16 +00:00
2016-06-08 23:06:41 +01:00
const builtIn = {
port: 8000,
2016-06-09 22:54:57 +01:00
directory: process.cwd()
}
2016-06-08 23:06:41 +01:00
if (options.server.rewrite) {
options.server.rewrite = parseRewriteRules(options.server.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
/* override built-in defaults with stored config and then command line args */
options.server = Object.assign(builtIn, stored, options.server)
2015-11-12 23:02:38 +00:00
2016-06-08 23:06:41 +01:00
validateOptions(options)
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 => {
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
return {
from: matches[1],
to: matches[2]
}
})
}
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