Browse Source

/get endpoint - controller and sanitizer

pull/35/head
Harish Krishna 10 years ago
parent
commit
fb6947e3fe
  1. 5
      app.js
  2. 44
      controller/get.js
  3. 39
      sanitiser/_id.js
  4. 23
      sanitiser/get.js

5
app.js

@ -11,6 +11,7 @@ app.use( require('./middleware/jsonp') );
/** ----------------------- sanitisers ----------------------- **/ /** ----------------------- sanitisers ----------------------- **/
var sanitisers = {}; var sanitisers = {};
sanitisers.get = require('./sanitiser/get');
sanitisers.suggest = require('./sanitiser/suggest'); sanitisers.suggest = require('./sanitiser/suggest');
sanitisers.search = sanitisers.suggest; sanitisers.search = sanitisers.suggest;
sanitisers.reverse = require('./sanitiser/reverse'); sanitisers.reverse = require('./sanitiser/reverse');
@ -19,6 +20,7 @@ sanitisers.reverse = require('./sanitiser/reverse');
var controllers = {}; var controllers = {};
controllers.index = require('./controller/index'); controllers.index = require('./controller/index');
controllers.get = require('./controller/get');
controllers.suggest = require('./controller/suggest'); controllers.suggest = require('./controller/suggest');
controllers.search = require('./controller/search'); controllers.search = require('./controller/search');
@ -27,6 +29,9 @@ controllers.search = require('./controller/search');
// api root // api root
app.get( '/', controllers.index() ); app.get( '/', controllers.index() );
// get doc API
app.get( '/get', sanitisers.get.middleware, controllers.get() );
// suggest API // suggest API
app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() );

44
controller/get.js

@ -0,0 +1,44 @@
var geojsonify = require('../helper/geojsonify').search;
function setup( backend ){
// allow overriding of dependencies
backend = backend || require('../src/backend');
function controller( req, res, next ){
// backend command
var cmd = {
index: 'pelias',
type: req.clean.type,
id: req.clean.id
};
// query backend
backend().client.get( cmd, function( err, data ){
var docs = [];
// handle backend errors
if( err ){ return next( err ); }
if( data && data.found && data._source ){
docs.push(data._source);
}
// convert docs to geojson
var geojson = geojsonify( docs );
// response envelope
geojson.date = new Date().getTime();
// respond
return res.status(200).json( geojson );
});
}
return controller;
}
module.exports = setup;

39
sanitiser/_id.js

@ -0,0 +1,39 @@
// validate inputs, convert types and apply defaults
// id generally looks like 'geoname/4163334' (type/id)
// so, both type and id are required fields.
function sanitize( req ){
req.clean = req.clean || {};
var params= req.query;
// ensure params is a valid object
if( Object.prototype.toString.call( params ) !== '[object Object]' ){
params = {};
}
var errormessage = function(fieldname) {
return {
'error': true,
'message': 'invalid param \''+ fieldname + '\': text length, must be >0'
}
};
// id text
if('string' !== typeof params.id || !params.id.length){
return errormessage('id');
}
req.clean.id = params.id;
// type text
if('string' !== typeof params.type || !params.type.length){
return errormessage('type');
}
req.clean.type = params.type;
return { 'error': false };
}
// export function
module.exports = sanitize;

23
sanitiser/get.js

@ -0,0 +1,23 @@
var logger = require('../src/logger'),
_sanitize = require('../sanitiser/_sanitize'),
sanitizers = {
id: require('../sanitiser/_id')
};
var sanitize = function(req, cb) { _sanitize(req, sanitizers, cb); }
// export sanitize for testing
module.exports.sanitize = sanitize;
// middleware
module.exports.middleware = function( req, res, next ){
sanitize( req, function( err, clean ){
if( err ){
res.status(400); // 400 Bad Request
return next(err);
}
req.clean = clean;
next();
});
};
Loading…
Cancel
Save