refactor
This commit is contained in:
28
bin/cli.js
28
bin/cli.js
@ -3,18 +3,18 @@
|
||||
const Cli = require('../')
|
||||
|
||||
const ws = new Cli()
|
||||
ws.middleware.addCors()
|
||||
ws.middleware.addJson()
|
||||
ws.middleware.addRewrite()
|
||||
ws.middleware.addBodyParser()
|
||||
ws.middleware.addBlacklist()
|
||||
ws.middleware.addCache()
|
||||
ws.middleware.addMimeType()
|
||||
ws.middleware.addCompression()
|
||||
ws.middleware.addLogging()
|
||||
ws.middleware.addMockResponses()
|
||||
ws.middleware.addSpa()
|
||||
|
||||
ws.middleware.addStatic()
|
||||
ws.middleware.addIndex()
|
||||
ws.middleware
|
||||
.addCors()
|
||||
.addJson()
|
||||
.addRewrite()
|
||||
.addBodyParser()
|
||||
.addBlacklist()
|
||||
.addCache()
|
||||
.addMimeType()
|
||||
.addCompression()
|
||||
.addLogging()
|
||||
.addMockResponses()
|
||||
.addSpa()
|
||||
.addStatic()
|
||||
.addIndex()
|
||||
ws.listen()
|
||||
|
@ -1,24 +1,16 @@
|
||||
'use strict'
|
||||
const Cli = require('../cli')
|
||||
const Cli = require('../')
|
||||
const cacheControl = require('koa-cache-control')
|
||||
const cliData = require('../lib/cli-data')
|
||||
|
||||
cliData.push({ name: 'black' })
|
||||
|
||||
const ws = new Cli({
|
||||
'no-cache': true,
|
||||
log: { format: 'dev' }
|
||||
})
|
||||
|
||||
ws.middleware.splice(
|
||||
ws.middleware.findIndex(m => m.name === 'mime-type'),
|
||||
1,
|
||||
{
|
||||
name: 'cache-control',
|
||||
create: convert(cacheControl({
|
||||
maxAge: 15
|
||||
}))
|
||||
}
|
||||
)
|
||||
cliData.optionDefinitions.push({ name: 'maxage', group: 'misc' })
|
||||
|
||||
const ws = new Cli()
|
||||
ws.middleware
|
||||
.addLogging('dev')
|
||||
.add(cacheControl({
|
||||
maxAge: 15
|
||||
}))
|
||||
.addStatic()
|
||||
.addIndex()
|
||||
ws.listen()
|
||||
|
@ -1,14 +1,10 @@
|
||||
'use strict'
|
||||
const Koa = require('koa')
|
||||
const localWebServer = require('../')
|
||||
const Cli = require('../')
|
||||
const liveReload = require('koa-livereload')
|
||||
const convert = require('koa-convert')
|
||||
|
||||
const app = new Koa()
|
||||
const ws = localWebServer({
|
||||
log: { format: 'dev' }
|
||||
})
|
||||
|
||||
app.use(liveReload())
|
||||
app.use(ws)
|
||||
app.listen(8000)
|
||||
const ws = new Cli()
|
||||
ws.middleware
|
||||
.addLogging('dev')
|
||||
.add(liveReload())
|
||||
.addStatic()
|
||||
ws.listen(8000)
|
||||
|
@ -8,12 +8,12 @@ const Tool = require('command-line-tool')
|
||||
const tool = new Tool()
|
||||
|
||||
class Cli {
|
||||
constructor () {
|
||||
constructor (options) {
|
||||
this.options = null
|
||||
this.app = null
|
||||
this.middleware = null
|
||||
|
||||
let options = collectOptions()
|
||||
options = collectOptions()
|
||||
this.options = options
|
||||
|
||||
if (options.misc.config) {
|
||||
@ -24,23 +24,8 @@ class Cli {
|
||||
this.app = app
|
||||
|
||||
const MiddlewareStack = require('./middleware-stack')
|
||||
this.middleware = new MiddlewareStack({
|
||||
static: {
|
||||
root: options.server.directory,
|
||||
options: {
|
||||
hidden: true
|
||||
}
|
||||
},
|
||||
serveIndex: {
|
||||
path: options.server.directory,
|
||||
options: {
|
||||
icons: true,
|
||||
hidden: true
|
||||
}
|
||||
},
|
||||
log: {
|
||||
format: options.server['log-format']
|
||||
},
|
||||
this.middleware = new MiddlewareStack(options)
|
||||
const a = {
|
||||
compress: options.server.compress,
|
||||
mime: options.server.mime,
|
||||
forbid: options.server.forbid,
|
||||
@ -49,7 +34,7 @@ class Cli {
|
||||
rewrite: options.server.rewrite,
|
||||
verbose: options.server.verbose,
|
||||
mocks: options.server.mocks
|
||||
})
|
||||
}
|
||||
|
||||
app.on('error', err => {
|
||||
if (options.server['log-format']) {
|
||||
@ -121,9 +106,7 @@ function collectOptions () {
|
||||
|
||||
const builtIn = {
|
||||
port: 8000,
|
||||
directory: process.cwd(),
|
||||
forbid: [],
|
||||
rewrite: []
|
||||
directory: process.cwd()
|
||||
}
|
||||
|
||||
if (options.server.rewrite) {
|
||||
|
@ -8,17 +8,11 @@ const mw = require('./middleware')
|
||||
class MiddlewareStack extends Array {
|
||||
constructor (options) {
|
||||
super()
|
||||
this.options = options
|
||||
|
||||
options = Object.assign({
|
||||
static: {},
|
||||
serveIndex: {
|
||||
options: {
|
||||
icons: true,
|
||||
hidden: true
|
||||
}
|
||||
},
|
||||
cacheControl: {},
|
||||
spa: null,
|
||||
log: {},
|
||||
compress: false,
|
||||
mime: {},
|
||||
forbid: [],
|
||||
@ -31,20 +25,14 @@ class MiddlewareStack extends Array {
|
||||
process.env.DEBUG = '*'
|
||||
}
|
||||
|
||||
const log = options.log
|
||||
log.options = log.options || {}
|
||||
|
||||
if (options.verbose && !log.format) {
|
||||
log.format = 'none'
|
||||
}
|
||||
this.log = log
|
||||
|
||||
if (!options.static.root) options.static.root = process.cwd()
|
||||
if (!options.serveIndex.path) options.serveIndex.path = process.cwd()
|
||||
options.rewrite = arrayify(options.rewrite)
|
||||
options.forbid = arrayify(options.forbid)
|
||||
options.mocks = arrayify(options.mocks)
|
||||
this.options = options
|
||||
}
|
||||
|
||||
add (middleware) {
|
||||
this.push(middleware)
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
@ -63,14 +51,13 @@ class MiddlewareStack extends Array {
|
||||
|
||||
/* rewrite rules */
|
||||
addRewrite () {
|
||||
const _ = require('koa-route')
|
||||
|
||||
const options = this.options.rewrite
|
||||
if (options.length) {
|
||||
options.forEach(route => {
|
||||
if (route.to) {
|
||||
/* `to` address is remote if the url specifies a host */
|
||||
if (url.parse(route.to).host) {
|
||||
const _ = require('koa-route')
|
||||
debug('proxy rewrite', `${route.from} -> ${route.to}`)
|
||||
this.push(_.all(route.from, mw.proxyRequest(route)))
|
||||
} else {
|
||||
@ -89,6 +76,7 @@ class MiddlewareStack extends Array {
|
||||
See https://github.com/nodejitsu/node-http-proxy/issues/180. */
|
||||
addBodyParser () {
|
||||
this.push(require('koa-bodyparser')())
|
||||
return this
|
||||
}
|
||||
|
||||
/* path blacklist */
|
||||
@ -98,6 +86,7 @@ class MiddlewareStack extends Array {
|
||||
debug('forbid', options.join(', '))
|
||||
this.push(mw.blacklist(options))
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/* cache */
|
||||
@ -106,6 +95,7 @@ class MiddlewareStack extends Array {
|
||||
this.push(require('koa-conditional-get')())
|
||||
this.push(require('koa-etag')())
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/* mime-type overrides */
|
||||
@ -115,6 +105,7 @@ class MiddlewareStack extends Array {
|
||||
debug('mime override', JSON.stringify(options))
|
||||
this.push(mw.mime(options))
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/* compress response */
|
||||
@ -124,28 +115,36 @@ class MiddlewareStack extends Array {
|
||||
debug('compression', 'enabled')
|
||||
this.push(compress())
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/* Logging */
|
||||
addLogging () {
|
||||
const log = this.log
|
||||
if (log.format !== 'none') {
|
||||
addLogging (format, options) {
|
||||
format = this.options.server['log-format'] || format
|
||||
options = options || {}
|
||||
|
||||
if (this.options.verbose && !format) {
|
||||
format = 'none'
|
||||
}
|
||||
|
||||
if (format !== 'none') {
|
||||
const morgan = require('koa-morgan')
|
||||
|
||||
if (!log.format) {
|
||||
if (!format) {
|
||||
const streamLogStats = require('stream-log-stats')
|
||||
log.options.stream = streamLogStats({ refreshRate: 500 })
|
||||
this.push(morgan('common', log.options))
|
||||
} else if (log.format === 'logstalgia') {
|
||||
options.stream = streamLogStats({ refreshRate: 500 })
|
||||
this.push(morgan('common', options))
|
||||
} else if (format === 'logstalgia') {
|
||||
morgan.token('date', () => {
|
||||
var d = new Date()
|
||||
return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '')
|
||||
})
|
||||
this.push(morgan('combined', log.options))
|
||||
this.push(morgan('combined', options))
|
||||
} else {
|
||||
this.push(morgan(log.format, log.options))
|
||||
this.push(morgan(format, options))
|
||||
}
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/* Mock Responses */
|
||||
@ -166,6 +165,7 @@ class MiddlewareStack extends Array {
|
||||
this.push(mw.mockResponses(mock.route, mock.target))
|
||||
}
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
/* for any URL not matched by static (e.g. `/search`), serve the SPA */
|
||||
@ -178,24 +178,27 @@ class MiddlewareStack extends Array {
|
||||
verbose: this.options.verbose
|
||||
}))
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/* serve static files */
|
||||
addStatic () {
|
||||
const options = this.options.static
|
||||
if (options.root) {
|
||||
addStatic (root, options) {
|
||||
root = this.options.server.directory || root || process.cwd()
|
||||
options = Object.assign({ hidden: true }, options)
|
||||
if (root) {
|
||||
const serve = require('koa-static')
|
||||
this.push(serve(options.root, options.options))
|
||||
this.push(serve(root, options))
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
/* serve directory index */
|
||||
addIndex () {
|
||||
const options = this.options.serveIndex
|
||||
if (options.path) {
|
||||
addIndex (path, options) {
|
||||
path = this.options.server.directory || path || process.cwd()
|
||||
options = Object.assign({ icons: true, hidden: true }, options)
|
||||
if (path) {
|
||||
const serveIndex = require('koa-serve-index')
|
||||
this.push(serveIndex(options.path, options.options))
|
||||
this.push(serveIndex(path, options))
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
Reference in New Issue
Block a user