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