Skip to content

Commit

Permalink
Merge pull request geddy#128 from ekryski/mongo-session
Browse files Browse the repository at this point in the history
Mongo session
  • Loading branch information
mde committed Apr 28, 2012
2 parents f05b36d + fafc458 commit 7b74c1d
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 6 deletions.
8 changes: 7 additions & 1 deletion examples/todo_app_mongo/config/development.js
Expand Up @@ -22,7 +22,13 @@ var config = {
, hostname: null
, port: 4000
, sessions: {
store: 'memory'
server: {
host : 'localhost'
, port : 27017
, db : 'testDB'
, collection : 'sessions'
}
, store: 'mongodb'
, key: 'sid'
, expiry: 14 * 24 * 60 * 60
}
Expand Down
114 changes: 114 additions & 0 deletions lib/sessions/stores/mongodb.js
@@ -0,0 +1,114 @@
/*
* Geddy JavaScript Web development framework
* Copyright 2012 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

/*
Config section should look like this:
, sessions: {
store: 'mongodb'
, server: {
host : 'localhost'
, port : 27017
, db : 'testDB'
, collection : 'sessions'
, user : "tom" // [optional] user
, password: 'superpass' //[optional] password
}
, key: 'sid'
, expiry: 14 * 24 * 60 * 60
'server' is your MongoDB server.
If don't provide an expiry in your config file your sessions will live forever.
*/


try {
mongo = require('mongodb-wrapper');
}
catch(e) {
throw new Error('The mongodb wrapper module could not be found. Try doing "npm install mongodb-wrapper."');
}

var MongoDB = function (callback) {
this.setup(callback);
};

MongoDB.prototype = new (function () {
var _sessions = {};

var _client;
var _key;
var sessionCollection;

var self = this;

this.setup = function (callback) {
var host = geddy.config.sessions.server.host || 'localhost';
var port = geddy.config.sessions.server.port || 27017;
var db = geddy.config.sessions.server.db || 'sessionDB';
var prefix = geddy.config.sessions.server.prefix;
var user = geddy.config.sessions.server.user || 'user';
var password = geddy.config.sessions.server.password || 'pass';

sessionCollection = geddy.config.sessions.server.collection || 'sessions';
_key = geddy.config.sessions.key || 'sid';

//Authentication will not work until mongodb-wrapper is updated. A fix
//was just merged to master on April 18th, 2012
_client = mongo.db(host, port, db, prefix, user, password);
_client.collection(sessionCollection);

//Mongo-wrapper offers lazy open/close so we can't really check if the client
//exists because it always will exist.
callback();
};

this.read = function (session, callback) {
var query = {};
query[_key] = session.id;
_client[sessionCollection].findOne(query, function (err, result) {
if (err) {
geddy.log.error(err);
}
callback(result || {});
});
};

this.write = function (sess, callback) {
var session = sess.data;
session[_key] = sess.id;

if (geddy.config.sessions.expiry) {
session.expiry = geddy.config.sessions.expiry;
}

_client[sessionCollection].save(session, function (err, result) {
if (err) {
geddy.log.error('foo' + err);
}

callback();
});
};

})();

exports.Mongodb = MongoDB;


9 changes: 4 additions & 5 deletions lib/sessions/stores/redis.js
@@ -1,6 +1,6 @@
/*
* Geddy JavaScript Web development framework
* Copyright 2112 Matthew Eernisse (mde@fleegix.org)
* Copyright 2012 Matthew Eernisse (mde@fleegix.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,8 +34,6 @@ Config section should look like this:
'server' is your Redis server.
*/

var Redis;

try {
RedisServer = require('redis');
}
Expand All @@ -51,17 +49,18 @@ Redis.prototype = new (function () {
var _sessions = {};

var _client;
var _ns;

var self = this;

this.setup = function (callback) {
var port = geddy.config.sessions.server.posrt || null;
var port = geddy.config.sessions.server.port || null;
var host = geddy.config.sessions.server.host || null;
var opts = geddy.config.sessions.server.opts || {};
_ns = geddy.config.sessions.server.ns || "";
_client = RedisServer.createClient(port, host, opts);
_client.on("error", function (err) {
sys.error("Redis error " + client.host + ":" + client.port + " - " + err);
sys.error("Redis error " + _client.host + ":" + _client.port + " - " + err);
});

if (geddy.config.sessions.server.auth) {
Expand Down

0 comments on commit 7b74c1d

Please sign in to comment.