relocate and rename Storage.js. implement a simple, non-persistent in memory datastore for those who'd rather not bother with mongodb. Continue to default to previous values.

This commit is contained in:
ansuz 2015-10-25 19:35:25 -04:00
parent 05ce2695b2
commit 3928c89d35
5 changed files with 65 additions and 5 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ www/bower_components/*
node_modules
/config.js
customization
.*.swp

View file

@ -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',

View file

@ -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);

48
storage/amnesia.js Normal file
View file

@ -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,
});
};

View file

@ -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; }