Browse Source

Move to simpler routing model

pull/213/head
Julian Simioni 9 years ago
parent
commit
826ad3b569
  1. 25
      middleware/semver.js
  2. 2
      package.json
  3. 35
      routes/legacy.js
  4. 20
      routes/v1.js

25
middleware/semver.js

@ -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;

2
package.json

@ -37,11 +37,11 @@
"async": "^0.9.0", "async": "^0.9.0",
"cluster2": "git://github.com/missinglink/cluster2.git#node_zero_twelve", "cluster2": "git://github.com/missinglink/cluster2.git#node_zero_twelve",
"express": "^4.8.8", "express": "^4.8.8",
"express-http-proxy": "^0.6.0",
"extend": "2.0.1", "extend": "2.0.1",
"geojson": "^0.2.1", "geojson": "^0.2.1",
"geojson-extent": "^0.3.1", "geojson-extent": "^0.3.1",
"geopipes-elasticsearch-backend": "^0.2.0", "geopipes-elasticsearch-backend": "^0.2.0",
"http-proxy": "^1.11.1",
"is-object": "^1.0.1", "is-object": "^1.0.1",
"markdown": "0.5.0", "markdown": "0.5.0",
"microtime": "1.4.0", "microtime": "1.4.0",

35
routes/legacy.js

@ -1,36 +1,29 @@
/** ----------------------- sanitisers ----------------------- **/ var proxy = require('express-http-proxy');
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');
/** ----------------------- controllers ----------------------- **/ var sendToLegacy = proxy('pelias.mapzen.com', {
forwardPath: function(req, res) {
var controllers = {}; return require('url').parse(req.url).path;
controllers.index = require('../controller/index'); }
controllers.place = require('../controller/place'); });
controllers.search = require('../controller/search');
function addRoutes(app) { function addRoutes(app) {
// api root // api root
app.get( '/', controllers.index() ); app.get( '/', sendToLegacy );
// place API // place API
app.get( '/place', sanitisers.place.middleware, controllers.place() ); app.get( '/place', sendToLegacy);
// suggest APIs // suggest APIs
app.get( '/suggest', sanitisers.search.middleware, controllers.search() ); app.get( '/suggest', sendToLegacy );
app.get( '/suggest/nearby', sanitisers.suggest.middleware, controllers.search() ); app.get( '/suggest/nearby', sendToLegacy );
app.get( '/suggest/coarse', sanitisers.coarse.middleware, controllers.search() ); app.get( '/suggest/coarse',sendToLegacy );
// search APIs // search APIs
app.get( '/search', sanitisers.search.middleware, controllers.search() ); app.get( '/search', sendToLegacy);
app.get( '/search/coarse', sanitisers.coarse.middleware, controllers.search() ); app.get( '/search/coarse', sendToLegacy);
// reverse API // reverse API
app.get( '/reverse', sanitisers.reverse.middleware, controllers.search(undefined, require('../query/reverse')) ); app.get( '/reverse', sendToLegacy );
} }
module.exports.addRoutes = addRoutes; module.exports.addRoutes = addRoutes;

20
routes/v1.js

@ -6,10 +6,6 @@ sanitisers.search = require('../sanitiser/search');
sanitisers.coarse = require('../sanitiser/coarse'); sanitisers.coarse = require('../sanitiser/coarse');
sanitisers.reverse = require('../sanitiser/reverse'); sanitisers.reverse = require('../sanitiser/reverse');
/** ---------------------- routing --------------------------- **/
var routers = {};
routers.semver = require('../middleware/semver');
/** ----------------------- controllers ----------------------- **/ /** ----------------------- controllers ----------------------- **/
var controllers = {}; var controllers = {};
@ -19,22 +15,22 @@ controllers.search = require('../controller/search');
function addRoutes(app, peliasConfig) { function addRoutes(app, peliasConfig) {
// api root // api root
app.get( '/:vr/', controllers.index() ); app.get( '/v1/', controllers.index() );
// place API // place API
app.get( '/:vr/place', sanitisers.place.middleware, controllers.place() ); app.get( '/v1/place', sanitisers.place.middleware, controllers.place() );
// suggest APIs // suggest APIs
app.get( '/:vr/suggest', sanitisers.search.middleware, controllers.search() ); app.get( '/v1/suggest', sanitisers.search.middleware, controllers.search() );
app.get( '/:vr/suggest/nearby', sanitisers.suggest.middleware, controllers.search() ); app.get( '/v1/suggest/nearby', sanitisers.suggest.middleware, controllers.search() );
app.get( '/:vr/suggest/coarse', sanitisers.coarse.middleware, controllers.search() ); app.get( '/v1/suggest/coarse', sanitisers.coarse.middleware, controllers.search() );
// search APIs // search APIs
app.get( '/:vr/search', routers.semver(peliasConfig), sanitisers.search.middleware, controllers.search() ); app.get( '/v1/search', sanitisers.search.middleware, controllers.search() );
app.get( '/:vr/search/coarse', sanitisers.coarse.middleware, controllers.search() ); app.get( '/v1/search/coarse', sanitisers.coarse.middleware, controllers.search() );
// reverse API // 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; module.exports.addRoutes = addRoutes;

Loading…
Cancel
Save