From 826ad3b569e3863c58150ec4a565aa02b151d6e2 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Tue, 1 Sep 2015 14:51:41 -0400 Subject: [PATCH] Move to simpler routing model --- middleware/semver.js | 25 ------------------------- package.json | 2 +- routes/legacy.js | 35 ++++++++++++++--------------------- routes/v1.js | 20 ++++++++------------ 4 files changed, 23 insertions(+), 59 deletions(-) delete mode 100644 middleware/semver.js diff --git a/middleware/semver.js b/middleware/semver.js deleted file mode 100644 index 5b98e4b9..00000000 --- a/middleware/semver.js +++ /dev/null @@ -1,25 +0,0 @@ -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 ce1e1b15..8eb00ce7 100644 --- a/package.json +++ b/package.json @@ -37,11 +37,11 @@ "async": "^0.9.0", "cluster2": "git://github.com/missinglink/cluster2.git#node_zero_twelve", "express": "^4.8.8", + "express-http-proxy": "^0.6.0", "extend": "2.0.1", "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", diff --git a/routes/legacy.js b/routes/legacy.js index b5bcc6e1..d7fbd0d2 100644 --- a/routes/legacy.js +++ b/routes/legacy.js @@ -1,36 +1,29 @@ -/** ----------------------- sanitisers ----------------------- **/ -var sanitisers = {}; -sanitisers.place = require('../sanitiser/place'); -sanitisers.suggest = require('../sanitiser/suggest'); -sanitisers.search = require('../sanitiser/search'); -sanitisers.coarse = require('../sanitiser/coarse'); -sanitisers.reverse = require('../sanitiser/reverse'); +var proxy = require('express-http-proxy'); -/** ----------------------- controllers ----------------------- **/ - -var controllers = {}; -controllers.index = require('../controller/index'); -controllers.place = require('../controller/place'); -controllers.search = require('../controller/search'); +var sendToLegacy = proxy('pelias.mapzen.com', { + forwardPath: function(req, res) { + return require('url').parse(req.url).path; + } +}); function addRoutes(app) { // api root - app.get( '/', controllers.index() ); + app.get( '/', sendToLegacy ); // place API - app.get( '/place', sanitisers.place.middleware, controllers.place() ); + app.get( '/place', sendToLegacy); // suggest APIs - app.get( '/suggest', sanitisers.search.middleware, controllers.search() ); - app.get( '/suggest/nearby', sanitisers.suggest.middleware, controllers.search() ); - app.get( '/suggest/coarse', sanitisers.coarse.middleware, controllers.search() ); + app.get( '/suggest', sendToLegacy ); + app.get( '/suggest/nearby', sendToLegacy ); + app.get( '/suggest/coarse',sendToLegacy ); // search APIs - app.get( '/search', sanitisers.search.middleware, controllers.search() ); - app.get( '/search/coarse', sanitisers.coarse.middleware, controllers.search() ); + app.get( '/search', sendToLegacy); + app.get( '/search/coarse', sendToLegacy); // reverse API - app.get( '/reverse', sanitisers.reverse.middleware, controllers.search(undefined, require('../query/reverse')) ); + app.get( '/reverse', sendToLegacy ); } module.exports.addRoutes = addRoutes; diff --git a/routes/v1.js b/routes/v1.js index 5c57646c..dddc993d 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -6,10 +6,6 @@ sanitisers.search = require('../sanitiser/search'); sanitisers.coarse = require('../sanitiser/coarse'); sanitisers.reverse = require('../sanitiser/reverse'); -/** ---------------------- routing --------------------------- **/ -var routers = {}; -routers.semver = require('../middleware/semver'); - /** ----------------------- controllers ----------------------- **/ var controllers = {}; @@ -19,22 +15,22 @@ controllers.search = require('../controller/search'); function addRoutes(app, peliasConfig) { // api root - app.get( '/:vr/', controllers.index() ); + app.get( '/v1/', controllers.index() ); // place API - app.get( '/:vr/place', sanitisers.place.middleware, controllers.place() ); + app.get( '/v1/place', sanitisers.place.middleware, controllers.place() ); // suggest APIs - app.get( '/:vr/suggest', sanitisers.search.middleware, controllers.search() ); - app.get( '/:vr/suggest/nearby', sanitisers.suggest.middleware, controllers.search() ); - app.get( '/:vr/suggest/coarse', sanitisers.coarse.middleware, controllers.search() ); + app.get( '/v1/suggest', sanitisers.search.middleware, controllers.search() ); + app.get( '/v1/suggest/nearby', sanitisers.suggest.middleware, controllers.search() ); + app.get( '/v1/suggest/coarse', sanitisers.coarse.middleware, controllers.search() ); // search APIs - app.get( '/:vr/search', routers.semver(peliasConfig), sanitisers.search.middleware, controllers.search() ); - app.get( '/:vr/search/coarse', sanitisers.coarse.middleware, controllers.search() ); + app.get( '/v1/search', sanitisers.search.middleware, controllers.search() ); + app.get( '/v1/search/coarse', sanitisers.coarse.middleware, controllers.search() ); // reverse API - app.get( '/:vr/reverse', sanitisers.reverse.middleware, controllers.search(undefined, require('../query/reverse')) ); + app.get( '/v1/reverse', sanitisers.reverse.middleware, controllers.search(undefined, require('../query/reverse')) ); } module.exports.addRoutes = addRoutes;