first-pass at proxyRoutes

This commit is contained in:
Lloyd Brookes
2015-11-15 11:59:45 +00:00
parent b116426640
commit 7bfbeaa030
6 changed files with 132 additions and 17 deletions

View File

@ -10,6 +10,10 @@ const streamLogStats = require('stream-log-stats')
const cors = require('kcors')
const conditional = require('koa-conditional-get');
const etag = require('koa-etag');
const _ = require('koa-route')
const mount = require('koa-mount')
const httpProxy = require('http-proxy')
const pathToRegexp = require('path-to-regexp')
/**
* @module local-web-server
@ -22,16 +26,88 @@ function getApp (options) {
serveIndex: {},
log: {},
compress: false,
blacklist: []
blacklist: [],
directories: [],
proxyRoutes: []
}, options)
const log = options.log
log.options = log.options || {}
const app = new Koa()
const _use = app.use
app.use = x => _use.call(app, convert(x))
const proxy = httpProxy.createProxyServer({
changeOrigin: true
})
// app.use(_.all('/api/*', function * (apiPath) {
// this.response = false
// proxy.once('proxyReq', function (proxyReq, req, res, options) {
// proxyReq.path = `http://registry.npmjs.org/${apiPath}`;
// })
// proxy.web(this.req, this.res, { target: `http://registry.npmjs.org/${apiPath}` })
// }))
// app.use(mount('/gh', function * (next) {
// this.response = false
// proxy.web(this.req, this.res, { target: 'https://api.github.com' })
// }))
// app.use(_.get('/:one/gh/:two', function * (one, two) {
// this.response = false
// proxy.once('proxyReq', function (proxyReq, req, res, options) {
// proxyReq.path = `https://api.github.com/${one}/${two}`;
// })
// proxy.web(this.req, this.res, { target: `https://api.github.com/${one}/${two}` })
// }))
// app.use(_.get('/*/yeah/:one/*', function * (one, two) {
// console.log(arguments);
// this.response = false
// proxy.once('proxyReq', function (proxyReq, req, res, options) {
// proxyReq.path = `https://api.github.com/${one}/${two}`;
// })
// proxy.web(this.req, this.res, { target: `https://api.github.com/${one}/${two}` })
// }))
// const proxyRoutes = [
// // { mount: '/api', to: 'http://registry.npmjs.org' },
// // { mount: '/gh', to: 'http://https://api.github.com' },
// { from: '/:one/gh/:two', to: 'https://api.github.com/${one}/${two}' },
// { from: '/api/*', to: 'http://registry.npmjs.org/${0}' },
// ]
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] || '')
// console.log('==========');
// console.log(arguments);
// console.log(re);
// console.log(index);
// console.log(route);
})
proxy.once('proxyReq', function (proxyReq) {
proxyReq.path = route.new;
})
proxy.web(this.req, this.res, { target: route.new })
}))
})
/* CORS: allow from any origin */
app.use(convert(cors()))
app.use(cors())
/* path blacklist */
if (options.blacklist.length) {
@ -44,8 +120,8 @@ function getApp (options) {
})
}
app.use(convert(conditional()))
app.use(convert(etag()))
app.use(conditional())
app.use(etag())
/* mime-type overrides */
if (options.mime) {
@ -62,7 +138,7 @@ function getApp (options) {
/* compress response */
if (options.compress) {
app.use(convert(compress()))
app.use(compress())
}
/* special case log formats */
@ -78,16 +154,24 @@ function getApp (options) {
log.format = 'common'
log.options.stream = streamLogStats({ refreshRate: 500 })
}
if (log.format) app.use(convert(morgan.middleware(log.format, log.options)))
if (log.format) app.use(morgan.middleware(log.format, log.options))
// options.static.root = [
// { route: '/one', root: 'lib' },
// { route: '/two', root: 'node_modules' }
// ]
/* serve static files */
if (options.static.root) {
app.use(convert(serve(options.static.root, options.static.options)))
app.use(serve(options.static.root, options.static.options))
// options.static.root.forEach(config => {
// app.use(mount(config.route, serve(config.root)))
// app.use(mount(config.route, serveIndex(config.root)))
// })
}
/* serve directory index */
if (options.serveIndex.path) {
app.use(convert(serveIndex(options.serveIndex.path, options.serveIndex.options)))
app.use(serveIndex(options.serveIndex.path, options.serveIndex.options))
}
return app