diff --git a/middleware/semver.js b/middleware/semver.js new file mode 100644 index 00000000..5b98e4b9 --- /dev/null +++ b/middleware/semver.js @@ -0,0 +1,25 @@ +var httpProxy = require('http-proxy'); + +var proxy = new httpProxy.createProxyServer(); + +function setup(peliasConfig) { + var currentVersion = peliasConfig.version; //TODO :add to pelias-config + + var middleware = function middleware(req, res, next){ + var reqVersion = req.params.vr; + // if URI contains v1, handle locally + if (currentVersion === reqVersion) { // uri contains v1 + next(); + } else if (peliasConfig.proxyMap.hasOwnProperty(reqVersion)){ // else send to legacy server + var host = peliasConfig.proxyMap[reqVersion]; + req.url = req.url.substring(1 + reqVersion.length); + proxy.proxyRequest(req, res, { target: host }); + } else { + throw new Error('can\'t handle this version'); + } + }; + + return middleware; +} + +module.exports = setup; diff --git a/package.json b/package.json index a9c3c7e1..ce1e1b15 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "geojson": "^0.2.1", "geojson-extent": "^0.3.1", "geopipes-elasticsearch-backend": "^0.2.0", + "http-proxy": "^1.11.1", "is-object": "^1.0.1", "markdown": "0.5.0", "microtime": "1.4.0",