Browse Source

Merge branch 'master' of github.com:pelias/api into responses

pull/210/head
Diana Shkolnikov 9 years ago
parent
commit
985bfb5911
  1. 62
      app.js
  2. 1
      package.json
  3. 29
      routes/legacy.js
  4. 90
      routes/v1.js

62
app.js

@ -1,6 +1,4 @@
var Router = require('express').Router;
var app = require('express')();
var peliasConfig = require( 'pelias-config' ).generate().api;
@ -16,62 +14,12 @@ app.use( require('./middleware/headers') );
app.use( require('./middleware/cors') );
app.use( require('./middleware/jsonp') );
/**
* Helper function for creating routers
*
* @param {[{function}]} functions
* @returns {express.Router}
*/
function createRouter(functions) {
var router = Router(); // jshint ignore:line
functions.forEach(function (f) {
router.use(f);
});
return router;
}
var routers = {};
routers.search = createRouter([
require('./sanitiser/search').middleware,
require('./controller/search')()
]);
routers.reverse = createRouter([
require('./sanitiser/reverse').middleware,
require('./controller/search')(undefined, require('./query/reverse'))
]);
routers.place = createRouter([
require('./sanitiser/place').middleware,
require('./controller/place')()
]);
routers.index = createRouter([
require('./controller/index')()
]);
// api root
app.get( '/', routers.index );
app.get( '/place', routers.place );
app.get( '/autocomplete', routers.search );
app.get( '/search', routers.search);
app.post( '/search', routers.search);
app.get( '/reverse', routers.reverse );
/** -------------------- post-processing-middleware ------------------**/
// TODO: name mapping for admin values (admin0 => country, etc)
app.use(require('./middleware/renamePlacenames')());
app.use(require('./middleware/geocodeJSON')(peliasConfig));
app.use(require('./middleware/sendJSON'));
/** ----------------------- routes ----------------------- **/
var legacy = require('./routes/legacy');
legacy.addRoutes(app, peliasConfig);
var v1 = require('./routes/v1');
v1.addRoutes(app, peliasConfig);
/** ----------------------- error middleware ----------------------- **/

1
package.json

@ -37,6 +37,7 @@
"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",

29
routes/legacy.js

@ -0,0 +1,29 @@
var proxy = require('express-http-proxy');
function addRoutes(app, peliasConfig) {
if (!peliasConfig.hasOwnProperty('legacyUrl')) {
return;
}
var sendToLegacy = proxy(peliasConfig.legacyUrl);
// api root
app.get( '/', sendToLegacy );
// place API
app.get( '/place', sendToLegacy);
// suggest APIs
app.get( '/suggest', sendToLegacy );
app.get( '/suggest/nearby', sendToLegacy );
app.get( '/suggest/coarse',sendToLegacy );
// search APIs
app.get( '/search', sendToLegacy);
app.get( '/search/coarse', sendToLegacy);
// reverse API
app.get( '/reverse', sendToLegacy );
}
module.exports.addRoutes = addRoutes;

90
routes/v1.js

@ -0,0 +1,90 @@
var reverseQuery = require('./query/reverse');
/** ----------------------- sanitisers ----------------------- **/
var sanitisers = {
place: require('../sanitiser/place'),
search: require('../sanitiser/search'),
reverse: require('../sanitiser/reverse')
};
/** ----------------------- controllers ----------------------- **/
var controllers = {
index: require('../controller/index'),
place: require('../controller/place'),
search: require('../controller/search')
};
/** ----------------------- controllers ----------------------- **/
var postProc = {
renamePlacenames: require('./middleware/renamePlacenames'),
geocodeJSON: require('./middleware/geocodeJSON'),
sendJSON: require('./middleware/sendJSON')
};
/**
* Append routes to app
*
* @param {object} app
* @param {object} peliasConfig
*/
function addRoutes(app, peliasConfig) {
/** ------------------------- routers ------------------------- **/
var routers = {
index: createRouter([
controllers.index()
]),
search: createRouter([
sanitisers.search.middleware,
controllers.search(),
postProc.renamePlacenames(),
postProc.geocodeJSON(peliasConfig),
postProc.sendJSON
]),
reverse: createRouter([
sanitisers.reverse.middleware,
controllers.search(undefined, reverseQuery),
postProc.renamePlacenames(),
postProc.geocodeJSON(peliasConfig),
postProc.sendJSON
]),
place: createRouter([
sanitisers.place.middleware,
controllers.place(),
postProc.renamePlacenames(),
postProc.geocodeJSON(peliasConfig),
postProc.sendJSON
])
};
var base = '/v1/';
// api root
app.get ( base, routers.index );
app.get ( base + 'place', routers.place );
app.get ( base + 'autocomplete', routers.search );
app.get ( base + 'search', routers.search );
app.post( base + 'search', routers.search );
app.get ( base + 'reverse', routers.reverse );
}
/**
* Helper function for creating routers
*
* @param {[{function}]} functions
* @returns {express.Router}
*/
function createRouter(functions) {
var router = Router(); // jshint ignore:line
functions.forEach(function (f) {
router.use(f);
});
return router;
}
module.exports.addRoutes = addRoutes;
Loading…
Cancel
Save