mirror of https://github.com/pelias/api.git
Julian Simioni
9 years ago
4 changed files with 23 additions and 59 deletions
@ -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; |
@ -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; |
||||
|
Loading…
Reference in new issue