|
|
@ -4,6 +4,8 @@ const serve = require('koa-static') |
|
|
|
const convert = require('koa-convert') |
|
|
|
const serveIndex = require('koa-serve-index') |
|
|
|
const morgan = require('koa-morgan') |
|
|
|
const compress = require('koa-compress') |
|
|
|
const streamLogStats = require('stream-log-stats') |
|
|
|
|
|
|
|
/** |
|
|
|
* @module local-web-server |
|
|
@ -18,19 +20,47 @@ function getApp (options) { |
|
|
|
options = Object.assign({ |
|
|
|
static: {}, |
|
|
|
serveIndex: {}, |
|
|
|
logger: {} |
|
|
|
logger: {}, |
|
|
|
compress: false |
|
|
|
}, options) |
|
|
|
|
|
|
|
const app = new Koa() |
|
|
|
|
|
|
|
if (options.compress) { |
|
|
|
console.log('comp'); |
|
|
|
app.use(convert(compress())) |
|
|
|
} |
|
|
|
|
|
|
|
let log = { format: options.logger.format } |
|
|
|
if (options.logger.format) { |
|
|
|
app.use(convert(morgan.middleware(options.logger.format, options.logger.options))) |
|
|
|
|
|
|
|
if (log.format === 'none'){ |
|
|
|
log.format = undefined |
|
|
|
} else if (log.format === 'logstalgia') { |
|
|
|
/* customised logger :date token, purely to satisfy Logstalgia. */ |
|
|
|
morgan.token('date', function () { |
|
|
|
var d = new Date() |
|
|
|
return (d.getDate() + '/' + d.getUTCMonth() + '/' + d.getFullYear() + ':' + d.toTimeString()) |
|
|
|
.replace('GMT', '').replace(' (BST)', '') |
|
|
|
}) |
|
|
|
log.format = 'combined' |
|
|
|
} else if (log.format) { |
|
|
|
log.stream = process.stdout |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.format = 'common' |
|
|
|
log.stream = streamLogStats({ refreshRate: 100 }) |
|
|
|
} |
|
|
|
options.logger.options = options.logger.options || {} |
|
|
|
options.logger.options.stream = log.stream |
|
|
|
if (log.format) app.use(convert(morgan.middleware(log.format, options.logger.options))) |
|
|
|
|
|
|
|
if (options.static.root) { |
|
|
|
app.use(convert(serve(options.static.root, options.static.options))) |
|
|
|
} |
|
|
|
if (options.serveIndex.path) { |
|
|
|
app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options))) |
|
|
|
} |
|
|
|
|
|
|
|
return app |
|
|
|
} |