From 7b32498d5bf823fc592f1c629d929a348c9b278c Mon Sep 17 00:00:00 2001 From: andreagerino Date: Mon, 27 Jun 2016 16:33:19 +0200 Subject: [PATCH] Editing Storage endpoint class to specify the mongodb connection URL as a parameter. Public storage endpoint added. --- app.js | 5 +- routes/storage.js | 143 ++++++++++++++++++++++++---------------------- 2 files changed, 78 insertions(+), 70 deletions(-) diff --git a/app.js b/app.js index c111385..b967002 100644 --- a/app.js +++ b/app.js @@ -3,7 +3,9 @@ var path = require('path'); var logger = require('morgan'); var bodyParser = require('body-parser'); -var storage = require('./routes/storage'); +var storage = require('./routes/storage')('mongodb://icarus:icarus@localhost:27017/icarus'); +var public_storage = require('./routes/storage')('mongodb://public:public@localhost:27017/public'); + var services = require('./routes/services'); var app = express(); @@ -31,6 +33,7 @@ app.use(function(req, res, next) { }); app.use('/icarus/str', storage); +app.use('/icarus/public', public_storage); app.use('/icarus/svc', services); // catch 404 and forward to error handler diff --git a/routes/storage.js b/routes/storage.js index 24a1408..abd6d04 100644 --- a/routes/storage.js +++ b/routes/storage.js @@ -1,5 +1,4 @@ var express = require('express'); -var router = express.Router(); var debug = require('debug')('Icarus:server'); var mongoClient = require('mongodb').MongoClient; @@ -7,123 +6,129 @@ var ObjectID = require('mongodb').ObjectID; var assert = assert = require('assert'); -// Connection URL -var mongoUrl = 'mongodb://icarus:icarus@localhost:27017/icarus'; +module.exports = ( -//Connect to the server and intialise the pool -var icarusDb; -mongoClient.connect(mongoUrl, function(err, db) { + function(mongoUrl){ - assert.equal(null, err, err); - debug("Connected to MongoDB database: "+db.databaseName); + var icarusDb; + var router = express.Router(); - icarusDb = db; + mongoClient.connect(mongoUrl , function(err, db) { -}); + assert.equal(null, err, err); + debug("Connected to MongoDB database: "+db.databaseName); -router.get('/:resource_id', function(req, res, next) { + icarusDb = db; - var collection = icarusDb.collection('resources'); + }); - var _id = new ObjectID(req.params.resource_id); - collection.find({"_id":_id}).limit(1).next(function(err, doc){ + router.get('/:resource_id', function(req, res, next) { - if(err!=null){ + var collection = icarusDb.collection('resources'); - debug(err); - next(err); + var _id = new ObjectID(req.params.resource_id); + collection.find({"_id":_id}).limit(1).next(function(err, doc){ - }else{ + if(err!=null){ - if(doc==undefined){ + debug(err); + next(err); - res.sendStatus(404); + }else{ - }else{ + if(doc==undefined){ - res.send(doc); + res.sendStatus(404); - } + }else{ - } + res.send(doc); - }); + } -}); + } -router.post('/', function(req, res, next){ + }); - var collection = icarusDb.collection('resources'); - var resource = req.body; + }); - if(Object.keys(resource).length>0) { + router.post('/', function(req, res, next){ - var timestampContainer = getTimestampContainer(resource); + var collection = icarusDb.collection('resources'); + var resource = req.body; - if(timestampContainer!=undefined){ + if(Object.keys(resource).length>0) { - preprocessTimestampContainer(timestampContainer); + var timestampContainer = getTimestampContainer(resource); - } + if(timestampContainer!=undefined){ - collection.insertOne(resource, function (err, r) { + preprocessTimestampContainer(timestampContainer); - if (err != null) { + } - debug(err); - next(err); + collection.insertOne(resource, function (err, r) { - } else { + if (err != null) { - res.statusCode = 201; - res.send({"_id":r.insertedId}); + debug(err); + next(err); - } + } else { - }); + res.statusCode = 201; + res.send({"_id":r.insertedId}); - }else{ + } - var error = new Error("You can't store an empty resource!"); - error.status = 400; + }); - debug(error); - next(error); + }else{ - } + var error = new Error("You can't store an empty resource!"); + error.status = 400; -}); + debug(error); + next(error); -function getTimestampContainer(arg){ + } - if(arg['timestamp']!=undefined&&(arg['timestamp']['utc']!=undefined||arg['timestamp']['user']!=undefined)) - return arg['timestamp']; + }); - return {}; + function getTimestampContainer(arg){ -} + if(arg['timestamp']!=undefined&&(arg['timestamp']['utc']!=undefined||arg['timestamp']['user']!=undefined)) + return arg['timestamp']; -function preprocessTimestampContainer(container){ + return {}; - var utcSeconds = container['utc']; - var userSeconds = container['user']; + } - if(utcSeconds!=undefined){ + function preprocessTimestampContainer(container){ - var utcMsec = utcSeconds * 1000; - container['utc'] = new Date(utcMsec); + var utcSeconds = container['utc']; + var userSeconds = container['user']; - } + if(utcSeconds!=undefined){ + + var utcMsec = utcSeconds * 1000; + container['utc'] = new Date(utcMsec); - if(userSeconds!=undefined){ + } - var userMsec = userSeconds * 1000; - container['user'] = new Date(userMsec); + if(userSeconds!=undefined){ - } + var userMsec = userSeconds * 1000; + container['user'] = new Date(userMsec); - container['server'] = new Date(); + } -} + container['server'] = new Date(); + + } + + return router; + + } -module.exports = router; +); -- 2.18.1