diff --git a/.gitignore b/.gitignore index fe33e3f97..f0aa1d7da 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ www/bower_components/* node_modules /config.js customization +.*.swp diff --git a/config.js.dist b/config.js.dist index 5cab8572a..5a21985cf 100644 --- a/config.js.dist +++ b/config.js.dist @@ -1,6 +1,7 @@ module.exports = { httpPort: 3000, websocketPort: 3001, + storage: './storage/mongo', mongoUri: "mongodb://demo_user:demo_password@ds027769.mongolab.com:27769/demo_database", // mongoUri: "mongodb://localhost:27017/cryptpad", mongoCollectionName: 'cryptpad', diff --git a/server.js b/server.js index ac586d936..5784e2292 100644 --- a/server.js +++ b/server.js @@ -4,11 +4,13 @@ var Https = require('https'); var Fs = require('fs'); var WebSocketServer = require('ws').Server; var ChainPadSrv = require('./ChainPadSrv'); -var Storage = require('./Storage'); var config = require('./config'); config.websocketPort = config.websocketPort || config.httpPort; +// support multiple storage back ends +var Storage = require(config.storage||'./storage/mongo'); + var app = Express(); app.use(Express.static(__dirname + '/www')); @@ -61,12 +63,14 @@ app.get('/api/config', function(req, res){ var httpServer = httpsOpts ? Https.createServer(httpsOpts, app) : Http.createServer(app); -httpServer.listen(config.httpPort); -console.log('listening on port ' + config.httpPort); +httpServer.listen(config.httpPort,config.httpAddress,function(){ + console.log('listening on %s',config.httpPort); +}); var wsConfig = { server: httpServer }; if (config.websocketPort !== config.httpPort) { - wsConfig = { port: config.websocketPort }; + console.log("setting up a new websocket server"); + wsConfig = { port: config.websocketPort}; } var wsSrv = new WebSocketServer(wsConfig); diff --git a/storage/amnesia.js b/storage/amnesia.js new file mode 100644 index 000000000..4024f7470 --- /dev/null +++ b/storage/amnesia.js @@ -0,0 +1,48 @@ +console.log("Loading amnesiadb. This is a horrible idea in production, as data *will not* persist\n"); + +/* + As the comment says, this module does nothing to make your data persist + across sessions. If your process crashes for any reason, all pads will die. + + This might be useful if you want to debug other parts of the codebase, if + you want to test out cryptpad without installing mongodb locally, or if + you don't want to rely on a remote db like the one at mongolab.com. + + Maybe you just like the idea of a forgetful pad? To use this module, edit + config.js to include a directive `storage: './storage/amnesia' + + Enjoy! +*/ + +var db=[], + index=0; + +var insert = function(channelName, content, cb){ + var val = { + id:index++, + chan: channelName, + msg: content, + time: new Date().getTime(), + }; + db.push(val); + cb(); +}; + +var getMessages = function(channelName, cb){ + db.sort(function(a,b){ + return a.id - b.id; + }); + db.filter(function(val){ + return val.chan == channelName; + }).forEach(function(doc){ + console.log(doc); + cb(doc.msg); + }); +}; + +module.exports.create = function(conf, cb){ + cb({ + message: insert, + getMessages: getMessages, + }); +}; diff --git a/Storage.js b/storage/mongo.js similarity index 89% rename from Storage.js rename to storage/mongo.js index 23f070afb..67f662a09 100644 --- a/Storage.js +++ b/storage/mongo.js @@ -22,6 +22,7 @@ var COLLECTION_NAME = 'cryptpad'; var insert = function (coll, channelName, content, cb) { var val = {chan: channelName, msg:content, time: (new Date()).getTime()}; coll.insertOne(val, {}, function (err, r) { + console.log(r); if (err || (r.insertedCount !== 1)) { console.log('failed to insert ' + err); return; @@ -31,7 +32,12 @@ var insert = function (coll, channelName, content, cb) { }; var getMessages = function (coll, channelName, cb) { - coll.find({chan:channelName}).sort( { _id: 1 } ).forEach(function (doc) { + // find entries with a matching channelname + coll.find({chan:channelName}) + // sort by _id, ascending + .sort( { _id: 1 } ) + // iterate over entries + .forEach(function (doc) { cb(doc.msg); }, function (err) { if (!err) { return; }