Permalink
Browse files

refactoring controllers out

  • Loading branch information...
dominictarr committed Jun 8, 2011
1 parent 53aedb7 commit 47324131594bbd795ef7805f079bd4384932b681
Showing with 123 additions and 80 deletions.
  1. +6 −0 controllers/index.js
  2. +8 −0 controllers/result.js
  3. +48 −0 controllers/summary.js
  4. +10 −2 handler.js
  5. +0 −1 initialize.js
  6. +5 −0 readme.markdown
  7. +38 −72 server.js
  8. +3 −5 test/handler.asynct.js
  9. +1 −0 views/404.jade
  10. +4 −0 views/500.jade
View
@@ -0,0 +1,6 @@
+
+module.exports = {
+ summary: require('./summary')
+, result: require('./result')
+//, summary: require('./summary')
+}
View
@@ -0,0 +1,8 @@
+
+module.exports =
+ function (req,cont){
+ db.get([req.params.username, req.params.project, req.params.commit].join(','),
+ function (err,data){
+ cont(null,data)
+ })
+ }
View
@@ -0,0 +1,48 @@
+
+function summary(opts,cont){
+
+ db.view('all/summary',opts, function (err,data){
+ if(err) {
+ console.error(err)
+ res.statusCode = 500
+ return cont(err)
+ }
+ data.rows.sort(function (x,y){
+ return x.value.time < y.value.time ? 1 : -1
+ })
+ if(data.rows.length)
+ cont(null,data)
+ else
+ cont(404)
+
+ })
+}
+
+
+module.exports = {
+ home: function (req,cont){
+ var opts = {
+ group_level: 2,
+ reduce: true
+ }
+ summary(opts,cont)
+ },
+ username:
+ function (req,cont){
+ var opts = {
+ startkey: [req.params.username,'_______'],
+ endkey: [req.params.username,'ZZZZZZZZZ'],
+ group_level: 2,
+ reduce: true
+ }
+ summary(opts,cont)
+ },
+ username_project: function (req,cont){
+ var opts = {
+ endkey: [req.params.username,req.params.project,'ZZZZZZZ'],
+ startkey: [req.params.username,req.params.project,'_____'],
+ reduce: false
+ }
+ summary(opts,cont)
+ }
+}
View
@@ -1,5 +1,13 @@
var it = require('it-is')
+/*
+
+ objects are always sent to render as {self: object}
+
+ in the view use `self.property`, instead of `property`
+
+*/
+
module.exports =
function Handler (){
@@ -9,7 +17,7 @@ function Handler (){
if(!handler.error[status])
status = 500
if('string' == typeof handler.error[status])
- return res.render(handler.error[status], err)
+ return res.render(handler.error[status], {self: err})
handler.error[status](err,res)
}
@@ -23,7 +31,7 @@ function Handler (){
if(err) return handleError(err,res)
if('string' == typeof view) {
- res.render(view, obj, function (err,data){
+ res.render(view, {self: obj}, function (err,data){
if(err) return handleError(err)
res.send(data)
})
View
@@ -7,7 +7,6 @@ save ~/.testbedrc.json with configuration info.
var init = require('cradle-init')
-
module.exports = function (opts,callback){
opts.raw = true
opts.cache = true
View
@@ -5,5 +5,10 @@ planned features
* button to update (rerun test)
* add a repo from the web page. (this will require locking or some sort of job queue)
* listen to npm -- follow the changes api, then make webhooks with EventEmitter2
+
+
+Immediate:
+ * save output
+ * test views.
View
110 server.js
@@ -9,12 +9,17 @@ var testbed = new (require('./testbed'))(process.cwd() + '/workspace')
, url = require('url')
, db
, config
+ , handler = require('./handler')()
+ , controllers = require('./controllers')
+
+ handler.error['500'] = '500'
+ handler.error['404'] = '404'
w.cli()
w.info('TESTBED')
w.info(new Date)
- if(!module.parent){
+ if(!module.parent) {
config = require('./setup').deploy()
} else {
config = require('./setup').test()
@@ -25,21 +30,19 @@ var testbed = new (require('./testbed'))(process.cwd() + '/workspace')
if(!module.parent)
app.listen(config.port);
-
- db = require('./initialize')(config.database,function (err,db){
+ db = require('./initialize')(config.database, function (err, db){
if(err){
w.error("DATABASE SETUP ERROR")
throw err
}
w.info("DATABASE '" + config.database + "' IS READY")
})
-
-function save (repo){ //pull this out also.
+function save (repo) { //pull this out also.
if(repo._id && !repo.saving){
repo.saving = true
- db.get('' + repo._id, function (err,doc){
+ db.get('' + repo._id, function (err, doc){
var obj = {}
for (var key in repo)
@@ -59,7 +62,7 @@ function save (repo){ //pull this out also.
type: repo.type,
username: repo.username,
},
- function (err,data){
+ function (err, data){
repo._rev = data._rev
repo.saving = null
if(repo.changed){
@@ -73,7 +76,7 @@ function save (repo){ //pull this out also.
}
}
-app.post('/', function (req,res){
+app.post('/', function (req, res) {
//pull all of this out into a repo controller.
@@ -85,13 +88,13 @@ app.post('/', function (req,res){
w.error('could not parse POST JSON')
w.statusCode = 400
return res.send ({
- error: "was not valid JSON",
- was: req.body.payload,
- exception: err.stack})
+ error: "was not valid JSON",
+ was: req.body.payload,
+ exception: err.stack})
}
w.info('loading new repo')
eyes.inspect(payload)
-
+
repo = testbed.Repo(
payload.repository.owner.name,
payload.repository.name,
@@ -101,17 +104,15 @@ app.post('/', function (req,res){
repo.on('change',function (event){
//log
- console.log([repo.username,repo.project].join('/'),[].shift.call(arguments))
- while(arguments.length)
- console.log([].shift.call(arguments))
- console.log('--------------------------------')
+ console.log(event, repo._id)
save(repo)
})
repo.integrate(function (err,data){
//log
if(err){
- return res.send({error: err, reason: "could not connect to github or npm", data: data})
+ return res.send({error: err,
+ reason: "could not connect to github or npm", data: data})
}
res.send(data)
})
@@ -130,70 +131,35 @@ views for errors are defined once for the handler
//app.get('/path', handleRoute(controller, renderer))
-app.get('/:username/:project/:commit', function (req,res){
- db.get([req.params.username, req.params.project, req.params.commit].join(','),
- function (err,data){
- res.render('result',{self:data})
- })
-})
-
-function summary(opts,res){
+/*
+app.get(path, handler(controller, 'view'))
- db.view('all/summary',opts, function (err,data){
- if(err) {
- console.error(err)
- res.statusCode = 500
- return res.send(err)
- }
- data.rows.sort(function (x,y){
- return x.value.time < y.value.time ? 1 : -1
- })
- res.render('user',{self:data})
- })
+controller gets request object, and a callback
+function controller (req, callback){
+ //do what you gotta do, then callback.
+ if(err)
+ callback(err)
+ else
+ callback(err,obj)
}
-//replace this with a function that calls the decoupled controller with just
-//params, query,
-//and that calls back
-//with an the object ready for the view.
-
-app.get('/:username/:project', function (req,res){
-
- var key =
-
- console.log(key)
-
- var opts = {
- endkey: [req.params.username,req.params.project,'ZZZZZZZ'],
- startkey: [req.params.username,req.params.project,'_____'],
- reduce: false,
-// group: false
- }
+if it's an error handler calls error controller.
- summary(opts,res)
+else, handler calls view.
+res.render(view, obj, function (err,data){
+ if (err) log object and view to a file for testing later.
+ else
+ res.send(data)
})
-app.get('/:username', function (req,res){
-
- var opts = {
- startkey: [req.params.username,'_______'],
- endkey: [req.params.username,'ZZZZZZZZZ'],
- group_level: 2,
- reduce: true
- }
+*/
- summary(opts,res)
+app.get('/:username/:project/:commit', handler(controllers.result, 'result'))
-})
+app.get('/:username/:project', handler(controllers.summary.username_project, 'user'))
-app.get('/', function (req,res){
-
- var opts = {
- group_level: 2,
- reduce: true
- }
+app.get('/:username', handler(controllers.summary.username,'user'))
- summary(opts,res)
-})
+app.get('/', handler(controllers.summary.home,'user'))
View
@@ -16,7 +16,7 @@ exports ['pass call controller and then render'] = function (test){
render: function (view, obj, cb) {
console.log('called render')
it(view).equal('example')
- it(obj).deepEqual({hello: 'asdf'})
+ it(obj).deepEqual({self: {hello: 'asdf'}})
cb(null,'<h1> hello asdf </h1>')
},
send: function (content){
@@ -103,13 +103,11 @@ exports ['set error handler to a view name'] = function (test) {
console.log('called render', view, obj)
it(view).equal('500')
- it(obj).deepEqual({ error: 'NO GOOD' })
+ it(obj).deepEqual({self: { error: 'NO GOOD' }})
it(cb).equal(null)
test.done()
}
}
action({}, mockRes)
-
-}
-
+}
View
@@ -0,0 +1 @@
+h1 404! UH OH
View
@@ -0,0 +1,4 @@
+H1 oops! 500!
+pre!= JSON.stringify(self)
+div.content
+ a(href='http://github.com/dominictarr/testbed/issues') please lodge an issue on github.

0 comments on commit 4732413

Please sign in to comment.