Skip to content
Snippets Groups Projects
Commit 69482e42 authored by steven.liatti's avatar steven.liatti
Browse files

Add simple and ugly condition to run application in case of missing MongoDB

parent d1dd301c
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,13 @@ logger.level = 'debug'; ...@@ -17,6 +17,13 @@ logger.level = 'debug';
// Define the MongoDB connection // Define the MongoDB connection
const db = require('monk')(process.env.DB); const db = require('monk')(process.env.DB);
let useDb = false;
db.then(() => {
logger.info('Connection to MongoDB OK');
useDb = true;
}).catch((err) => {
logger.error('Connection to MongoDB failed');
});
const collection = db.get('kittens'); const collection = db.get('kittens');
// For simple purpose, laxist CORS rules // For simple purpose, laxist CORS rules
...@@ -106,8 +113,11 @@ app.get('/swagger.json', (req, res) => { ...@@ -106,8 +113,11 @@ app.get('/swagger.json', (req, res) => {
app.get('/:width/:height', async (req, res) => { app.get('/:width/:height', async (req, res) => {
logger.debug(req); logger.debug(req);
try { try {
let doc = undefined;
if (useDb) {
// If a matching doc is found, return it // If a matching doc is found, return it
const doc = await collection.findOne({ width: req.params.width, height: req.params.height }); doc = await collection.findOne({ width: req.params.width, height: req.params.height });
}
if (doc) { if (doc) {
doc._id = undefined; doc._id = undefined;
res.status(200).send(doc); res.status(200).send(doc);
...@@ -123,8 +133,10 @@ app.get('/:width/:height', async (req, res) => { ...@@ -123,8 +133,10 @@ app.get('/:width/:height', async (req, res) => {
width: req.params.width, width: req.params.width,
height: req.params.height height: req.params.height
} }
if (useDb) {
logger.debug('insert in DB', kitten);
collection.insert(kitten); collection.insert(kitten);
}
res.status(200).send(kitten); res.status(200).send(kitten);
} }
} catch (error) { } catch (error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment