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

163 lines
4.3 KiB
JavaScript
Raw Normal View History

2015-11-08 22:09:07 +00:00
'use strict'
2015-11-13 11:26:02 +00:00
const path = require('path')
2015-11-15 13:53:27 +00:00
const http = require('http')
const url = require('url')
2015-11-08 22:09:07 +00:00
const Koa = require('koa')
const serve = require('koa-static')
const convert = require('koa-convert')
const serveIndex = require('koa-serve-index')
2015-11-10 21:50:56 +00:00
const morgan = require('koa-morgan')
2015-11-12 23:02:38 +00:00
const compress = require('koa-compress')
const streamLogStats = require('stream-log-stats')
2015-11-13 11:42:16 +00:00
const cors = require('kcors')
2015-11-13 15:33:19 +00:00
const conditional = require('koa-conditional-get');
const etag = require('koa-etag');
2015-11-15 11:59:45 +00:00
const _ = require('koa-route')
const mount = require('koa-mount')
const httpProxy = require('http-proxy')
const pathToRegexp = require('path-to-regexp')
2015-11-15 14:53:25 +00:00
const send = require('koa-send')
2015-11-08 22:09:07 +00:00
/**
* @module local-web-server
*/
module.exports = getApp
function getApp (options) {
2015-11-10 21:50:56 +00:00
options = Object.assign({
static: {},
serveIndex: {},
log: {},
2015-11-13 19:55:52 +00:00
compress: false,
2015-11-15 11:59:45 +00:00
blacklist: [],
directories: [],
proxyRoutes: []
2015-11-08 22:09:07 +00:00
}, options)
const log = options.log
log.options = log.options || {}
2015-11-08 22:09:07 +00:00
const app = new Koa()
2015-11-15 11:59:45 +00:00
const _use = app.use
app.use = x => _use.call(app, convert(x))
const proxy = httpProxy.createProxyServer({
changeOrigin: true
})
2015-11-15 12:29:43 +00:00
/* Proxy routes */
2015-11-15 11:59:45 +00:00
options.proxyRoutes.forEach(route => {
app.use(_.all(route.from, function * () {
const keys = []
route.re = pathToRegexp(route.from, keys)
route.new = route.to
this.response = false
keys.forEach((key, index) => {
const re = {
token: RegExp('\\$\\{' + key.name + '\\}', 'g'),
index: RegExp('\\$\\{' + index + '\\}', 'g')
}
route.new = route.new
.replace(re.token, arguments[index] || '')
.replace(re.index, arguments[index] || '')
})
proxy.once('proxyReq', function (proxyReq) {
2015-11-15 13:53:27 +00:00
proxyReq.path = url.parse(route.new).path;
2015-11-15 11:59:45 +00:00
})
proxy.web(this.req, this.res, { target: route.new })
}))
})
2015-11-08 22:09:07 +00:00
2015-11-13 11:42:16 +00:00
/* CORS: allow from any origin */
2015-11-15 11:59:45 +00:00
app.use(cors())
2015-11-13 11:26:02 +00:00
2015-11-13 15:33:19 +00:00
/* path blacklist */
2015-11-13 19:55:52 +00:00
if (options.blacklist.length) {
app.use(function pathBlacklist (ctx, next) {
if (options.blacklist.some(regexp => regexp.test(ctx.path))) {
2015-11-15 12:29:43 +00:00
ctx.throw(403, http.STATUS_CODES[403])
2015-11-13 19:55:52 +00:00
} else {
return next()
}
})
}
2015-11-13 15:33:19 +00:00
2015-11-15 15:51:18 +00:00
/* Cache */
if (!options['no-cache']) {
app.use(conditional())
app.use(etag())
}
2015-11-13 15:33:19 +00:00
/* mime-type overrides */
2015-11-13 11:26:02 +00:00
if (options.mime) {
app.use((ctx, next) => {
return next().then(() => {
const reqPathExtension = path.extname(ctx.path).slice(1)
Object.keys(options.mime).forEach(mimeType => {
const extsToOverride = options.mime[mimeType]
if (extsToOverride.indexOf(reqPathExtension) > -1) ctx.type = mimeType
})
})
})
}
2015-11-13 15:33:19 +00:00
/* compress response */
2015-11-12 23:02:38 +00:00
if (options.compress) {
2015-11-15 11:59:45 +00:00
app.use(compress())
2015-11-12 23:02:38 +00:00
}
/* special case log formats */
if (log.format) {
2015-11-12 23:02:38 +00:00
if (log.format === 'none'){
log.format = undefined
} else if (log.format === 'logstalgia') {
morgan.token('date', logstalgiaDate)
2015-11-12 23:02:38 +00:00
log.format = 'combined'
}
/* if no specific log format was requested, show log stats */
2015-11-12 23:02:38 +00:00
} else {
log.format = 'common'
log.options.stream = streamLogStats({ refreshRate: 500 })
2015-11-10 21:50:56 +00:00
}
2015-11-15 11:59:45 +00:00
if (log.format) app.use(morgan.middleware(log.format, log.options))
2015-11-12 23:02:38 +00:00
2015-11-15 11:59:45 +00:00
// options.static.root = [
// { route: '/one', root: 'lib' },
// { route: '/two', root: 'node_modules' }
// ]
2015-11-13 15:33:19 +00:00
/* serve static files */
2015-11-10 21:50:56 +00:00
if (options.static.root) {
2015-11-15 11:59:45 +00:00
app.use(serve(options.static.root, options.static.options))
2015-11-15 14:53:25 +00:00
2015-11-15 11:59:45 +00:00
// options.static.root.forEach(config => {
// app.use(mount(config.route, serve(config.root)))
// app.use(mount(config.route, serveIndex(config.root)))
// })
2015-11-10 21:50:56 +00:00
}
2015-11-13 15:33:19 +00:00
/* serve directory index */
2015-11-10 21:50:56 +00:00
if (options.serveIndex.path) {
2015-11-15 11:59:45 +00:00
app.use(serveIndex(options.serveIndex.path, options.serveIndex.options))
2015-11-10 21:50:56 +00:00
}
2015-11-12 23:02:38 +00:00
2015-11-15 14:53:25 +00:00
/* for any URL not matched by static (e.g. `/search`), serve the SPA */
if (options.spa) {
app.use(_.all('*', function * () {
yield send(this, options.spa, { root: process.cwd() })
}))
}
2015-11-08 22:09:07 +00:00
return app
}
function logstalgiaDate () {
var d = new Date()
return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`)
.replace('GMT', '')
.replace(' (BST)', '')
}
2015-11-13 15:33:19 +00:00
process.on('unhandledRejection', (reason, p) => {
throw reason
})