diff --git a/app.js b/app.js index c111385b9c567c167dfe4a20f0e3e370c65913e5..b967002751d85579ee2a111a5dd83f754d92bece 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 24a140855e0eebf83397425b77c38b1e5a2cec06..abd6d040b03d69550427668841dfcd05fec676bb 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; +);