Commit 7b32498d authored by andreagerino's avatar andreagerino

Editing Storage endpoint class to specify the mongodb connection URL as a...

Editing Storage endpoint class to specify the mongodb connection URL as a parameter. Public storage endpoint added.
parent c73328ae
...@@ -3,7 +3,9 @@ var path = require('path'); ...@@ -3,7 +3,9 @@ var path = require('path');
var logger = require('morgan'); var logger = require('morgan');
var bodyParser = require('body-parser'); 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 services = require('./routes/services');
var app = express(); var app = express();
...@@ -31,6 +33,7 @@ app.use(function(req, res, next) { ...@@ -31,6 +33,7 @@ app.use(function(req, res, next) {
}); });
app.use('/icarus/str', storage); app.use('/icarus/str', storage);
app.use('/icarus/public', public_storage);
app.use('/icarus/svc', services); app.use('/icarus/svc', services);
// catch 404 and forward to error handler // catch 404 and forward to error handler
......
var express = require('express'); var express = require('express');
var router = express.Router();
var debug = require('debug')('Icarus:server'); var debug = require('debug')('Icarus:server');
var mongoClient = require('mongodb').MongoClient; var mongoClient = require('mongodb').MongoClient;
...@@ -7,123 +6,129 @@ var ObjectID = require('mongodb').ObjectID; ...@@ -7,123 +6,129 @@ var ObjectID = require('mongodb').ObjectID;
var assert = assert = require('assert'); var assert = assert = require('assert');
// Connection URL module.exports = (
var mongoUrl = 'mongodb://icarus:icarus@localhost:27017/icarus';
//Connect to the server and intialise the pool function(mongoUrl){
var icarusDb;
mongoClient.connect(mongoUrl, function(err, db) {
assert.equal(null, err, err); var icarusDb;
debug("Connected to MongoDB database: "+db.databaseName); 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); router.get('/:resource_id', function(req, res, next) {
collection.find({"_id":_id}).limit(1).next(function(err, doc){
if(err!=null){ var collection = icarusDb.collection('resources');
debug(err); var _id = new ObjectID(req.params.resource_id);
next(err); 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); collection.insertOne(resource, function (err, r) {
next(err);
} else { if (err != null) {
res.statusCode = 201; debug(err);
res.send({"_id":r.insertedId}); 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); }else{
next(error);
} 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; var utcSeconds = container['utc'];
container['utc'] = new Date(utcMsec); var userSeconds = container['user'];
} if(utcSeconds!=undefined){
var utcMsec = utcSeconds * 1000;
container['utc'] = new Date(utcMsec);
if(userSeconds!=undefined){ }
var userMsec = userSeconds * 1000; if(userSeconds!=undefined){
container['user'] = new Date(userMsec);
} var userMsec = userSeconds * 1000;
container['user'] = new Date(userMsec);
container['server'] = new Date(); }
} container['server'] = new Date();
}
return router;
}
module.exports = router; );
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