Skip to content

Commit

Permalink
trying to make changes session based not quite there yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
jspears committed Apr 12, 2012
1 parent fd114c8 commit f3ddefa
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
4 changes: 3 additions & 1 deletion examples/simple/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ app.configure(function () {
// app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({secret:'super duper secret'}))
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});

app.configure('development', function () {
app.use('/bobamo', bobamo.express({uri:'mongodb://localhost/bobamo_development'}, express));
app.use('/bobamo', bobamo.express({plugin:'session', uri:'mongodb://localhost/bobamo_development'}, express));

app.use(express.errorHandler({ dumpExceptions:true, showStack:true }));
});
Expand Down
4 changes: 2 additions & 2 deletions lib/display-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function Model(modelName, args) {
this.modelName = modelName;
[ 'description'].forEach(easyget(args), this);

var _paths = this._paths = {};
var _paths = {};
args.forEach(function (v, k) {
_u.each(v.paths, function onPathArgs(vv, kk) {
(_paths[kk] || (_paths[kk] = [])).push(vv);
Expand All @@ -120,7 +120,7 @@ function Model(modelName, args) {

this.__defineGetter__('paths', function onPathsGetter() {
var ret = {};
_u.each(this._paths, function onPaths(v, k) {
_u.each(_paths, function onPaths(v, k) {
var field = ret[k] = new Field(k, v);
}, this);
return ret;
Expand Down
1 change: 0 additions & 1 deletion lib/plugin-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ var Plugin = function (options, app, name, p, pluginManager) {
});
this.baseUrl = this.options.baseUrl;
this.name = name || this.options.name || path.basename(path.dirname(module.parent.filename));
console.log('name', this.name);
this.__defineGetter__('baseUrl', function () {
var ret = _baseUrl;
return ret;
Expand Down
4 changes: 2 additions & 2 deletions lib/plugin-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ var path = require('path'), _u = require('underscore'), DisplayModel = require('
*/
var PluginManager = function (options, express) {
this.options = _u.extend({}, options);
this.plugins = this.loadPlugins(this.options, express);
this.appModel = new DisplayModel(this.options);
this.configFile = this.options.configFile || path.join(process.cwd, 'conf', 'bobamo.json');
this.persist = options.persist || new FilePersistence(this.configFile);
this.plugins = this.loadPlugins(this.options, express);
this.appModel = new DisplayModel(this.options);
this.loadAppModels();
this.loadFilters();
this.loadRoutes();
Expand Down
62 changes: 62 additions & 0 deletions plugins/session/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
var PluginApi = require('../../index').PluginApi, util = require('util'), crypto = require('crypto');
var SessionPersistence = function (orig) {
this.orig = orig;
}

SessionPersistence.prototype.save = function (key, data, callback) {
if (this.session) {
var conf = (this.session.conf || (this.session.conf = {plugins:{}})).plugins[key] = data;

var conf_str = JSON.stringify(conf);
var sha = crypto.createHash('sha1').update(conf_str).digest('base64');

callback(null, {_id:sha, timestamp:Date.now()});
}else{
callback(null,null);
}

}
SessionPersistence.prototype.list = function (callback) {
if (this.orig)
return this.orig.list(callback);
}

SessionPersistence.prototype.read = function (filename) {
if (this.session) {
return this.session.conf;
}else{
if (this.orig)
return this.orig.read(filename);
}
}

var SessionPlugin = function () {
PluginApi.apply(this, arguments);
var persist = this.persist = this.pluginManager.persist = new SessionPersistence(this.pluginManager.persist);

this._appModel = {};

['title', 'version', 'description', 'modelPaths'].forEach(function (v) {
this._appModel.__defineGetter__(v, function () {
var conf = persist.read();
if (conf && v in conf)
return conf[v];
return
});
}, this)
}

util.inherits(SessionPlugin, PluginApi);
module.exports = SessionPlugin;

SessionPlugin.prototype.appModel = function () {
return this._appModel;
}

SessionPlugin.prototype.filters = function () {
this.app.all(this.baseUrl + '*', function (req, res, next) {
this.persist.session = req.session;
next();
}.bind(this));
};

0 comments on commit f3ddefa

Please sign in to comment.