Skip to content
This repository has been archived by the owner on Sep 13, 2018. It is now read-only.

Commit

Permalink
Added couchdb store for session
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Jul 13, 2012
1 parent 09b48cb commit 72a589a
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 16 deletions.
18 changes: 12 additions & 6 deletions example/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,28 @@ bullet.init(app, __dirname);

app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/app/views');
app.set('views', app.views);
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));

app.use(express.favicon(app.favicon, app.config.get('cache')));
app.use(express.responseTime());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(express.cookieParser());
app.use(express.session(app.session));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.use(express.static(app.static, app.config.get('cache')));
});

app.configure('development', function(){
app.use(express.logger('dev'));
app.use(express.errorHandler());
});

app.configure('production', function(){
app.use(express.logger('default'));
});

http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
10 changes: 10 additions & 0 deletions example/config/development.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
{
"cache": {
"maxAge": 0
},
"db": {
"host": "localhost",
"port": 5984,
"database": "bullet"
},
"session": {
"key": "bullet.sid",
"secret": "bullletsecret",
"cookie": {
"maxAge": 31536000000
}
}
}
17 changes: 17 additions & 0 deletions example/config/production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"cache": {
"maxAge": 31536000000
},
"db": {
"host": "localhost",
"port": 5984,
"database": "bullet"
},
"session": {
"key": "bullet.sid",
"secret": "bullletsecret",
"cookie": {
"maxAge": 31536000000
}
}
}
3 changes: 3 additions & 0 deletions lib/bullet.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ bullet.init = function (app, pwd) {
app.static = path.join(pwd, 'public');
app.views = path.join(pwd, 'app', 'views');

app.session = app.config.get('session');

require('./error')(app);
require('./model')(app, pwd);
require('./controller')(app, pwd);
require('./director')(app);
require('./view')(app);
require('./session')(app);

return app;
};
Expand Down
4 changes: 2 additions & 2 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ Dispatch._checkAction = function (action) {
return true;
}
} else {
console.log('No action named ' + action[1] + ' in presenter ' + action[0]);
console.log('No action named ' + action[1] + ' in controller ' + action[0]);
}
} else {
console.log('No presenter named ' + action[0]);
console.log('No controller named ' + action[0]);
}
} else {
console.log('Wrong action format ' + action.join('/'));
Expand Down
141 changes: 141 additions & 0 deletions lib/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
var connect = require('connect')
, resourceful = require('resourceful');

module.exports = function (app) {

var CouchStore = function (options) {
options = options || {};
connect.session.Store.call(this, options);

var config = app.config.get('db');
config.database = config.database + '-sessions';

app.m.Session = resourceful.define('session', function () {
this.use('couchdb', config);

this.object('data');

this.filter('expires', {
map: function (doc) {
if (doc.resource == 'Session') {
emit(doc.data.cookie.expires, {id: doc._id });
}
}
});
});

var that = this;
that.db = app.m.Session.connection.connection;

that.db.exists(function (err, exists) {
if (!exists) {
that.db.create(function () {
that.db.query({ method: 'PUT', path: '_revs_limit', body: 5 }, function (){});
});
} else {
that.db.query({ method: 'PUT', path: '_revs_limit', body: 5 }, function (){});
}
});

this._compact = setInterval(this.compact, 300000);
this._reap = setInterval(this.reap, 86400000);
};

CouchStore.prototype.__proto__ = connect.session.Store.prototype;

/**
* Attempt to fetch session by the given `sid`.
*
* @param {String} sid
* @param {Function} fn
* @api public
*/
CouchStore.prototype.get = function (sid, fn) {
app.m.Session.get(sid, function (err, session) {
if (err) {
if (err.status == 404) err.code = 'ENOENT';
return fn && fn(err);
} else {
var expires = 'string' == typeof session.data.cookie.expires
? new Date(session.data.cookie.expires) : session.data.cookie.expires;

if (!session || !expires || new Date > expires) {
return fn && fn(null, null);
} else {
return fn(null, session.data);
}
}
});
};

/**
* Commit the given `sess` object associated with the given `sid`.
*
* @param {String} sid
* @param {Session} sess
* @param {Function} fn
* @api public
*/
CouchStore.prototype.set = function (sid, sess, fn) {
fn = fn || function () {};

app.m.Session.get(sid, function (err, session) {
if (err) {
session = app.m.Session.new({ id: sid, data: sess});
} else {
session.data = sess;
}
session.save(fn);
});
};

/**
* Destroy the session associated with the given `sid`.
*
* @param {String} sid
* @param {Function} fn
* @api public
*/
CouchStore.prototype.destroy = function (sid, fn) {
app.m.Session.destroy(sid, function (err, res) {
if (err) return fn && fn(err);
else fn();
});
};

CouchStore.prototype.clear = function (fn) {
var that = this;
that.db.destroy(function (err) {
if (err) return fn && fn(err);
that.db.create(fn);
});
};

CouchStore.prototype.length = function (fn) {
app.m.Session.expires({ startkey: (new Date()).toISOString() }, function (err, res) {
if (err) fn && fn(err);
else fn && fn(null, res.length);
});
};

CouchStore.prototype.compact = function () {
this.db.query({ method: 'POST', path: '_compact', body: {} }, function (err) {
if (err) console.log('Failed to compact -- ' + err);
});
};

CouchStore.prototype.reap = function () {
app.m.Session.expires({ endkey: (new Date()).toISOString() }, function (err, res) {
if (err) console.log('Failed to reap (view) -- ' + err);
resourceful.async.forEachSeries(res.map(function (r) {
return r.id.split('/').slice(1).join('/');
}), function (id, cb) {
app.m.Session.destroy(id, cb);
}, function (err) {
if (err) console.log('Failed to reap -- ' + err);
});
});
};

app.session.store = new CouchStore(app.config.get('session:store'));
};
4 changes: 2 additions & 2 deletions lib/template/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ app.configure(function(){
app.use(express.responseTime());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser(app.config.get('secret')));
app.use(express.session(app.config.get('cache')));
app.use(express.cookieParser());
app.use(express.session(app.session));
app.use(app.router);
app.use(express.static(app.static, app.config.get('cache')));
});
Expand Down
10 changes: 8 additions & 2 deletions lib/template/config/development.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"secret": "bullletsecret",
"cache": {
"maxAge": 31536000000
"maxAge": 0
},
"db": {
"host": "localhost",
"port": 5984,
"database": "bullet"
},
"session": {
"key": "bullet.sid",
"secret": "bullletsecret",
"cookie": {
"maxAge": 31536000000
}
}
}
8 changes: 7 additions & 1 deletion lib/template/config/production.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"secret": "bullletsecret",
"cache": {
"maxAge": 31536000000
},
"db": {
"host": "localhost",
"port": 5984,
"database": "bullet"
},
"session": {
"key": "bullet.sid",
"secret": "bullletsecret",
"cookie": {
"maxAge": 31536000000
}
}
}
9 changes: 6 additions & 3 deletions lib/template/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "application-name",
"name": "bullet",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "3.0.0beta3",
"jade": "*",
"express": "3.0.x",
"jade": "0.x.x",
"bullet": "0.1.x"
},
"engines": {
"node": "0.8.x"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
}
],
"dependencies": {
"connect": "2.3.x",
"utile": "0.1.x",
"nconf": "0.6.x",
"resourceful": "0.3.x",
Expand Down

0 comments on commit 72a589a

Please sign in to comment.