Commit c34f284f authored by Hervé Ménager's avatar Hervé Ménager

handle MongoDB not started, server side - send 503

parent b0ca4781
......@@ -5,11 +5,7 @@ var runQuery = function(queryFunction) {
var connString = 'mongodb://' + getConf('db.host', 'localhost') + ':' + getConf('db.port', '27017') + '/' + getConf('db.name', 'bioweb');
var serverOptions = getConf('db.serverOptions',{});
MongoClient.connect(connString, {server: serverOptions}, function(err, db) {
if(err){
throw err;
}else{
queryFunction(db, db.close.bind(db));
}
queryFunction(err, db);
});
};
......
......@@ -6,7 +6,18 @@ var getConf = require('./config');
var getSiteMap = require('./sitemap');
var getNewsFeed = require('./news-feed');
var runMongo = require('./mongo-helper').run;
var run = require('./mongo-helper').run;
var runMongo = function(res, queryFunction){
run(function(err, db, callback){
if(err){
res.sendStatus(503);
console.log('Cannot connect to the database.');
}else{
queryFunction(db, db.close.bind(db));
}
});
}
var router = function(app) {
......@@ -60,7 +71,7 @@ var router = function(app) {
'$in': ids
};
}
runMongo(function(db, close) {
runMongo(res, function(db, close) {
var edam = db.collection('edam');
if (req.query.search) {
filter.$text = {
......@@ -84,7 +95,7 @@ var router = function(app) {
/* get an EDAM term by ID */
app.get('/api/edam/:id', function(req, res, next) {
runMongo(function(db, close) {
runMongo(res, function(db, close) {
var edam = db.collection('edam');
edam.findOne({
'_id': req.params.id
......@@ -111,7 +122,7 @@ var router = function(app) {
callback = toFeed;
break;
}
runMongo(function(db, close) {
runMongo(res, function(db, close) {
var news = db.collection('news');
var limit = req.query.limit;
var query = [{ $sort : { "date" : -1}}];
......@@ -142,7 +153,7 @@ var router = function(app) {
if(req.query.count==='true'){
count = true;
}
runMongo(function(db, close) {
runMongo(res, function(db, close) {
var packagesIndex = db.collection('packagesIndex');
var edam = db.collection('edam');
var aggregatePipe = [
......@@ -164,7 +175,7 @@ var router = function(app) {
/* list/get package or databanks related entries (package, packageVersions, programs,
mobyles, galaxys, data) */
app.get('/api/:collection/:id?', function(req, res, next) {
runMongo(function(db, close) {
runMongo(res, function(db, close) {
var authorizedCollections = ['packages', 'packageVersions', 'programs'
, 'mobyles', 'galaxys', 'data'];
if(authorizedCollections.indexOf(req.params.collection)==-1){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment