From e8241902f3eb845f4f8f0084a5dc4613e1c6a968 Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Sat, 26 Nov 2011 23:57:00 -0800 Subject: [PATCH] settings key fixes --- config/app.json | 2 +- lib/app.js | 20 +++++++++++++++-- lib/db.js | 22 ++++++++++++------ lib/model.js | 45 +++++++++++++++++++++++++++++++------ lib/plugins/apps/app.js | 4 ++++ lib/plugins/apps/index.js | 15 +++---------- lib/plugins/models/index.js | 9 +++++--- lib/plugins/users/user.js | 2 +- lib/types.js | 2 ++ package.json | 3 ++- public/test/deployd.test.js | 2 +- 11 files changed, 91 insertions(+), 35 deletions(-) diff --git a/config/app.json b/config/app.json index 56afd7c..c715521 100644 --- a/config/app.json +++ b/config/app.json @@ -1,5 +1,5 @@ { "port": 3000, - "db-host": "mongodb://localhost/", + "db-host": "localhost", "db": "deployd" } \ No newline at end of file diff --git a/lib/app.js b/lib/app.js index e9bb4fd..97850ae 100644 --- a/lib/app.js +++ b/lib/app.js @@ -1,5 +1,7 @@ var express = require('express') , app = express.createServer() + , db = require('./db').db + , mongoStore = require('connect-mongodb'); ; // expose the app as a module so everyone can use it @@ -19,6 +21,7 @@ app.configure(function(){ if (method) { req.method = method.toUpperCase(); if(req.body && key in req.body) delete req.body[key]; + if(req.query && key in req.query) delete req.query[key]; // check X-HTTP-Method-Override } else if (req.headers['x-http-method-override']) { req.method = req.headers['x-http-method-override'].toUpperCase(); @@ -27,7 +30,12 @@ app.configure(function(){ next(); }); app.use(express.cookieParser()); - app.use(express.session({ secret: 'your secret here', key: 'deployd.sid', cookie: {httpOnly: false} })); + app.use(express.session({ + secret: 'secret', + key: 'deployd.sid', + cookie: {httpOnly: false}, + store: new mongoStore({db: db}) + })); app.use(app.router); app.use(express.static(__dirname + '/../public')); }); @@ -55,4 +63,12 @@ app.get('/routes', function(req, res) { app.get('/', function(req, res) { res.send({home: true}); -}); \ No newline at end of file +}); + +app.get('/dashboard', function(req, res, next) { + if(req.session && req.session.user && req.session.user.groups.root) { + next(); + } else { + res.send('Not Allowed.') + } +}); diff --git a/lib/db.js b/lib/db.js index fdf3d15..d74a0f5 100644 --- a/lib/db.js +++ b/lib/db.js @@ -2,23 +2,27 @@ var config = require('./config').load() , mongodb = require('mongodb') , ObjectID = require('mongodb').BSONPure.ObjectID , connectionString = config['db-host'] + config['db'] - , db + , Db = mongodb.Db + , Server = mongodb.Server + , server_config = new Server(config['db-host'], 27017, {auto_reconnect: true, native_parser: true}) + , db = new Db(config['db'], server_config, {}) ; + function ready(fn) { return function() { var _self = this , args = arguments ; - db + server_config.connected ? fn.apply(this, arguments) - : mongodb.connect(connectionString, function(err, _db) { - if(err || !_db) throw 'Could not connect to' + connectionString + ' (' + err + ')'; - db = _db; + : server_config.connect(db, function(err) { + if(err) console.log(err); + else { fn.apply(_self, args); - }) - ; + } + }) } } @@ -28,6 +32,8 @@ function collection(db, model, fn) { module.exports = { + db: db, + find: ready(function(model) { var query = model.toQuery() || {} , id = model.get('_id') || (query && query._id) @@ -52,11 +58,13 @@ module.exports = { , id = model.get('_id') , _id = id && ObjectID(id) , callback = function(err, result) { + if(err) model.error(err.message, err.name); model.refresh(changes); } ; if(_id && query) query._id = _id; + if(_id) changes._id = _id; collection(db, model, function(err, collection) { if(query) { diff --git a/lib/model.js b/lib/model.js index 979b128..4c6d540 100644 --- a/lib/model.js +++ b/lib/model.js @@ -343,6 +343,12 @@ Model = module.exports = emitter.spawn({ return this; }, + clean: function() { + delete this.errors; + this.attributes = {}; + return this; + }, + unlock: function() { // allow anyone to edit this specific model // this might be removed from the final API @@ -364,25 +370,51 @@ Model = module.exports = emitter.spawn({ settings .find({collection: this.collection, plugin: this.plugin}) .set({ - description: this.description, + // description: this.description, plugin: this.plugin, collection: this.collection, - name: this.name || this.collection, - allowed: this.allowed + name: this.name || this.collection + // allowed: this.allowed }) .notify(function(json) { if(json) { + // not found + if(json.errors) { + json = { + plugin: _self.plugin, + collection: _self.collection, + name: _self.name || _self.collection + }; + } + delete json._id; _self.description = _self.description || {}; - _self.description.extend(json.description); - + _self.allowed = _self.allowed || {}; + + // default to the models description + if(!json.description) json.description = _self.description; + // inherit existing description + else _self.description.extend(json.description); + + // default to the models allowed + if(!json.allowed) json.allowed = _self.allowed; + // inherit existing allowed + else _self.allowed.extend(json.allowed); + + // save updates + this + .clean() + .find({plugin: json.plugin, collection: json.collection}) + .set(json) + .save(); + // update any special properties _self.configure(_self.description, function(err) { if(err) console.error(err); }); } }) - .save() + .fetch() ; } @@ -421,7 +453,6 @@ Model = module.exports = emitter.spawn({ ; if(id && id.length < 24) return next(); - if(id && !query._id) query._id = id; model diff --git a/lib/plugins/apps/app.js b/lib/plugins/apps/app.js index d2997cd..1d08583 100644 --- a/lib/plugins/apps/app.js +++ b/lib/plugins/apps/app.js @@ -47,6 +47,10 @@ module.exports = Model.spawn({ return app.toLowerCase(); }, + defineRoutes: function() { + // TODO move routes here + }, + toDatabaseName: function() { return this.toHostName().replace(/\./g, ':').toLowerCase(); } diff --git a/lib/plugins/apps/index.js b/lib/plugins/apps/index.js index dbd6cc9..2ba06fb 100644 --- a/lib/plugins/apps/index.js +++ b/lib/plugins/apps/index.js @@ -1,5 +1,6 @@ var app = require('../../app') , App = require('./app') + , Apps = require('./apps') , Invite = require('./invite') ; @@ -7,7 +8,7 @@ if(process.argv.length < 3) { require('./balancer'); } -app.post('/app', function(req, res) { +app.post('/apps', function(req, res) { var session = req.session , me = session && session.user && session.user.email , secret = req.param('secret') @@ -35,17 +36,7 @@ app.post('/app', function(req, res) { }); -app.get('/app/:id', function(req, res) { - App - .spawn() - .for(req) - .set({_id: req.param('id')}) - .notify(res) - .fetch() - ; -}); - -app.del('/app/:id', function(req, res) { +app.del('/apps/:id', function(req, res) { App .spawn() .for(req) diff --git a/lib/plugins/models/index.js b/lib/plugins/models/index.js index 4d66ee1..b2d17e3 100644 --- a/lib/plugins/models/index.js +++ b/lib/plugins/models/index.js @@ -1,6 +1,8 @@ var Settings = require('../settings/settings') , Setting = require('../settings/setting') , Model = require('../../model') + , types = require('../../types').validators + , app = require('../../app') ; var models = module.exports.models = {}; @@ -19,6 +21,10 @@ var refresh = module.exports.refresh = function() { ; } +app.get('/types', function(req, res) { + res.send(Object.keys(types)); +}); + // initial refresh refresh(); @@ -29,9 +35,6 @@ Setting .set({ plugin: 'models' }) - .notify(function(json) { - console.log(json); - }) .save() ; diff --git a/lib/plugins/users/user.js b/lib/plugins/users/user.js index 2c86817..2c9647b 100644 --- a/lib/plugins/users/user.js +++ b/lib/plugins/users/user.js @@ -15,7 +15,7 @@ module.exports = Model.spawn({ auth: 'string', removed: 'boolean', groups: 'object' - }, + }, allowed: { read: 'public', diff --git a/lib/types.js b/lib/types.js index 8102e01..72820b3 100644 --- a/lib/types.js +++ b/lib/types.js @@ -52,6 +52,8 @@ var validators = { }; +exports.validators = validators; + // source can be a String, a validator Function(val), or an Object with a 'type' exports.compile = function(source) { var sourceType = typeof source diff --git a/package.json b/package.json index 4cf3b2f..629b764 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "underscore": ">= 1.0.0", "mongodb": ">= 0.9.0", "bouncy": ">= 1.0.2", - "ejs": ">= 0.4.3" + "ejs": ">= 0.4.3", + "connect-mongodb": ">= 1.0.0" }, "bin":{ "deployd":"./bin/deployd" diff --git a/public/test/deployd.test.js b/public/test/deployd.test.js index 48214d6..71a3b8f 100644 --- a/public/test/deployd.test.js +++ b/public/test/deployd.test.js @@ -75,7 +75,7 @@ var tests = { }, '7. creating an app': { - route: '/app', + route: '/apps', data: app, expect: { _id: 'toExist',