From 23e6cfbef782935d52e8990a84c6c2a49d57593e Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 15 Oct 2014 17:14:54 -0400 Subject: [PATCH] mget for real - first pass (experiencing problems with the backend client) --- app.js | 3 +++ controller/mget.js | 55 +++++++++++++++++++++++++++++++++++++++++++++ sanitiser/_ids.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++ sanitiser/mget.js | 23 +++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 controller/mget.js create mode 100644 sanitiser/_ids.js create mode 100644 sanitiser/mget.js diff --git a/app.js b/app.js index a80a4f82..1c071cb1 100644 --- a/app.js +++ b/app.js @@ -12,6 +12,7 @@ app.use( require('./middleware/jsonp') ); var sanitisers = {}; sanitisers.get = require('./sanitiser/get'); +sanitisers.mget = require('./sanitiser/mget'); sanitisers.suggest = require('./sanitiser/suggest'); sanitisers.search = sanitisers.suggest; sanitisers.reverse = require('./sanitiser/reverse'); @@ -21,6 +22,7 @@ sanitisers.reverse = require('./sanitiser/reverse'); var controllers = {}; controllers.index = require('./controller/index'); controllers.get = require('./controller/get'); +controllers.mget = require('./controller/mget'); controllers.suggest = require('./controller/suggest'); controllers.search = require('./controller/search'); @@ -31,6 +33,7 @@ app.get( '/', controllers.index() ); // get doc API app.get( '/get', sanitisers.get.middleware, controllers.get() ); +app.get( '/mget', sanitisers.mget.middleware, controllers.mget() ); // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); diff --git a/controller/mget.js b/controller/mget.js new file mode 100644 index 00000000..fadc4ede --- /dev/null +++ b/controller/mget.js @@ -0,0 +1,55 @@ + +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 = req.clean.ids.map( function(id) { + return { + index: 'pelias', + type: id.type, + id: id.id + }; + }); + cmd = { + index: 'pelias', + type: 'geoname', + ids: cmd.map(function(c){ return c.id }) + } + console.log('cmd:') + console.log(cmd) + // query backend + backend().client.mget( cmd, function( err, data ){ + console.log('error:') + console.log(err) + var docs = []; + // handle backend errors + if( err ){ return next( err ); } + + if( data && data.docs && Array.isArray(data.docs) && data.docs.length ){ + docs = data.docs.map( function( doc ){ + return doc._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; diff --git a/sanitiser/_ids.js b/sanitiser/_ids.js new file mode 100644 index 00000000..8354de9b --- /dev/null +++ b/sanitiser/_ids.js @@ -0,0 +1,56 @@ +// 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; + var indeces = require('../query/indeces'); + + // ensure params is a valid object + if( Object.prototype.toString.call( params ) !== '[object Object]' ){ + params = {}; + } + console.log(params) + + var errormessage = function(fieldname, message) { + return { + 'error': true, + 'message': message || ('invalid param \''+ fieldname + '\': text length, must be >0') + } + }; + + if( params && params.ids && params.ids.length ){ + req.clean.ids = []; + console.log(params.ids) + params.ids.split(',').forEach( function(param) { + param = param.split('/'); + var type= param[0]; + var id = param[1]; + console.log(param) + // id text + if('string' !== typeof id || !id.length){ + return errormessage('id'); + } + // type text + if('string' !== typeof type || !type.length){ + return errormessage('type'); + } + // type text must be one of the indeces + if(indeces.indexOf(type) == -1){ + return errormessage('type', 'type must be one of these values - [' + indeces.join(", ") + ']'); + } + req.clean.ids.push({ + id: id, + type: type + }); + }); + } + console.log(req.clean) + return { 'error': false }; + +} + +// export function +module.exports = sanitize; \ No newline at end of file diff --git a/sanitiser/mget.js b/sanitiser/mget.js new file mode 100644 index 00000000..b1f6d40f --- /dev/null +++ b/sanitiser/mget.js @@ -0,0 +1,23 @@ + +var logger = require('../src/logger'), + _sanitize = require('../sanitiser/_sanitize'), + sanitizers = { + id: require('../sanitiser/_ids') + }; + +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(); + }); +}; \ No newline at end of file