mirror of https://github.com/xwiki-labs/cryptpad
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:
parent
05ce2695b2
commit
3928c89d35
|
@ -2,3 +2,4 @@ www/bower_components/*
|
||||||
node_modules
|
node_modules
|
||||||
/config.js
|
/config.js
|
||||||
customization
|
customization
|
||||||
|
.*.swp
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
httpPort: 3000,
|
httpPort: 3000,
|
||||||
websocketPort: 3001,
|
websocketPort: 3001,
|
||||||
|
storage: './storage/mongo',
|
||||||
mongoUri: "mongodb://demo_user:demo_password@ds027769.mongolab.com:27769/demo_database",
|
mongoUri: "mongodb://demo_user:demo_password@ds027769.mongolab.com:27769/demo_database",
|
||||||
// mongoUri: "mongodb://localhost:27017/cryptpad",
|
// mongoUri: "mongodb://localhost:27017/cryptpad",
|
||||||
mongoCollectionName: 'cryptpad',
|
mongoCollectionName: 'cryptpad',
|
||||||
|
|
12
server.js
12
server.js
|
@ -4,11 +4,13 @@ var Https = require('https');
|
||||||
var Fs = require('fs');
|
var Fs = require('fs');
|
||||||
var WebSocketServer = require('ws').Server;
|
var WebSocketServer = require('ws').Server;
|
||||||
var ChainPadSrv = require('./ChainPadSrv');
|
var ChainPadSrv = require('./ChainPadSrv');
|
||||||
var Storage = require('./Storage');
|
|
||||||
|
|
||||||
var config = require('./config');
|
var config = require('./config');
|
||||||
config.websocketPort = config.websocketPort || config.httpPort;
|
config.websocketPort = config.websocketPort || config.httpPort;
|
||||||
|
|
||||||
|
// support multiple storage back ends
|
||||||
|
var Storage = require(config.storage||'./storage/mongo');
|
||||||
|
|
||||||
var app = Express();
|
var app = Express();
|
||||||
app.use(Express.static(__dirname + '/www'));
|
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);
|
var httpServer = httpsOpts ? Https.createServer(httpsOpts, app) : Http.createServer(app);
|
||||||
|
|
||||||
httpServer.listen(config.httpPort);
|
httpServer.listen(config.httpPort,config.httpAddress,function(){
|
||||||
console.log('listening on port ' + config.httpPort);
|
console.log('listening on %s',config.httpPort);
|
||||||
|
});
|
||||||
|
|
||||||
var wsConfig = { server: httpServer };
|
var wsConfig = { server: httpServer };
|
||||||
if (config.websocketPort !== config.httpPort) {
|
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);
|
var wsSrv = new WebSocketServer(wsConfig);
|
||||||
|
|
|
@ -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,
|
||||||
|
});
|
||||||
|
};
|
|
@ -22,6 +22,7 @@ var COLLECTION_NAME = 'cryptpad';
|
||||||
var insert = function (coll, channelName, content, cb) {
|
var insert = function (coll, channelName, content, cb) {
|
||||||
var val = {chan: channelName, msg:content, time: (new Date()).getTime()};
|
var val = {chan: channelName, msg:content, time: (new Date()).getTime()};
|
||||||
coll.insertOne(val, {}, function (err, r) {
|
coll.insertOne(val, {}, function (err, r) {
|
||||||
|
console.log(r);
|
||||||
if (err || (r.insertedCount !== 1)) {
|
if (err || (r.insertedCount !== 1)) {
|
||||||
console.log('failed to insert ' + err);
|
console.log('failed to insert ' + err);
|
||||||
return;
|
return;
|
||||||
|
@ -31,7 +32,12 @@ var insert = function (coll, channelName, content, cb) {
|
||||||
};
|
};
|
||||||
|
|
||||||
var getMessages = function (coll, channelName, 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);
|
cb(doc.msg);
|
||||||
}, function (err) {
|
}, function (err) {
|
||||||
if (!err) { return; }
|
if (!err) { return; }
|
Loading…
Reference in New Issue