|
@ -8,16 +8,14 @@ const morgan = require('koa-morgan') |
|
|
const compress = require('koa-compress') |
|
|
const compress = require('koa-compress') |
|
|
const streamLogStats = require('stream-log-stats') |
|
|
const streamLogStats = require('stream-log-stats') |
|
|
const cors = require('kcors') |
|
|
const cors = require('kcors') |
|
|
|
|
|
const conditional = require('koa-conditional-get'); |
|
|
|
|
|
const etag = require('koa-etag'); |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @module local-web-server |
|
|
* @module local-web-server |
|
|
*/ |
|
|
*/ |
|
|
module.exports = getApp |
|
|
module.exports = getApp |
|
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason, p) => { |
|
|
|
|
|
throw reason |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
function getApp (options) { |
|
|
function getApp (options) { |
|
|
options = Object.assign({ |
|
|
options = Object.assign({ |
|
|
static: {}, |
|
|
static: {}, |
|
@ -26,7 +24,6 @@ function getApp (options) { |
|
|
compress: false |
|
|
compress: false |
|
|
}, options) |
|
|
}, options) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const log = options.log |
|
|
const log = options.log |
|
|
log.options = log.options || {} |
|
|
log.options = log.options || {} |
|
|
|
|
|
|
|
@ -35,6 +32,19 @@ function getApp (options) { |
|
|
/* CORS: allow from any origin */ |
|
|
/* CORS: allow from any origin */ |
|
|
app.use(convert(cors())) |
|
|
app.use(convert(cors())) |
|
|
|
|
|
|
|
|
|
|
|
/* path blacklist */ |
|
|
|
|
|
app.use(function pathBlacklist (ctx, next) { |
|
|
|
|
|
if (/css$/.test(ctx.path)) { |
|
|
|
|
|
ctx.throw(403, 'FUCK NO.') |
|
|
|
|
|
} else { |
|
|
|
|
|
return next() |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
app.use(convert(conditional())) |
|
|
|
|
|
app.use(convert(etag())) |
|
|
|
|
|
|
|
|
|
|
|
/* mime-type overrides */ |
|
|
if (options.mime) { |
|
|
if (options.mime) { |
|
|
app.use((ctx, next) => { |
|
|
app.use((ctx, next) => { |
|
|
return next().then(() => { |
|
|
return next().then(() => { |
|
@ -47,6 +57,7 @@ function getApp (options) { |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* compress response */ |
|
|
if (options.compress) { |
|
|
if (options.compress) { |
|
|
app.use(convert(compress())) |
|
|
app.use(convert(compress())) |
|
|
} |
|
|
} |
|
@ -66,9 +77,12 @@ function getApp (options) { |
|
|
} |
|
|
} |
|
|
if (log.format) app.use(convert(morgan.middleware(log.format, log.options))) |
|
|
if (log.format) app.use(convert(morgan.middleware(log.format, log.options))) |
|
|
|
|
|
|
|
|
|
|
|
/* serve static files */ |
|
|
if (options.static.root) { |
|
|
if (options.static.root) { |
|
|
app.use(convert(serve(options.static.root, options.static.options))) |
|
|
app.use(convert(serve(options.static.root, options.static.options))) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* serve directory index */ |
|
|
if (options.serveIndex.path) { |
|
|
if (options.serveIndex.path) { |
|
|
app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options))) |
|
|
app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options))) |
|
|
} |
|
|
} |
|
@ -82,3 +96,7 @@ function logstalgiaDate () { |
|
|
.replace('GMT', '') |
|
|
.replace('GMT', '') |
|
|
.replace(' (BST)', '') |
|
|
.replace(' (BST)', '') |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason, p) => { |
|
|
|
|
|
throw reason |
|
|
|
|
|
}) |