Browse Source

mget for real - first pass (experiencing problems with the backend client)

pull/35/head
Harish Krishna 10 years ago
parent
commit
23e6cfbef7
  1. 3
      app.js
  2. 55
      controller/mget.js
  3. 56
      sanitiser/_ids.js
  4. 23
      sanitiser/mget.js

3
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() );

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

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

23
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();
});
};
Loading…
Cancel
Save