2014-06-03 10:14:10 -04:00
|
|
|
log = require 'winston'
|
|
|
|
errors = require '../commons/errors'
|
|
|
|
handlers = require('../commons/mapping').handlers
|
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
mongoose = require 'mongoose'
|
2014-06-03 16:54:56 -04:00
|
|
|
|
2014-06-03 10:14:10 -04:00
|
|
|
module.exports.setup = (app) ->
|
2014-06-03 16:54:56 -04:00
|
|
|
app.post '/admin/*', (req, res) ->
|
|
|
|
# TODO apparently I can leave this out as long as I use res.send
|
2014-06-03 10:14:10 -04:00
|
|
|
res.setHeader('Content-Type', 'application/json')
|
|
|
|
|
|
|
|
module = req.path[7..]
|
|
|
|
parts = module.split('/')
|
|
|
|
module = parts[0]
|
|
|
|
|
2014-06-24 12:14:26 -04:00
|
|
|
return errors.forbidden(res, 'Admins only') unless req.user?.isAdmin()
|
2014-06-03 10:14:10 -04:00
|
|
|
|
|
|
|
try
|
|
|
|
moduleName = module.replace '.', '_'
|
|
|
|
name = handlers[moduleName]
|
|
|
|
handler = require('../' + name)
|
|
|
|
|
2014-07-23 07:06:51 -04:00
|
|
|
return errors.notFound res, 'Handler not found for ' + moduleName unless handler
|
2014-06-03 10:14:10 -04:00
|
|
|
return handler[parts[1]](req, res, parts[2..]...) if parts[1] of handler
|
2014-07-23 07:06:51 -04:00
|
|
|
return errors.notFound res, 'Method not found for handler ' + name
|
2014-06-03 10:14:10 -04:00
|
|
|
|
|
|
|
catch error
|
|
|
|
log.error("Error trying db method '#{req.route.method}' route '#{parts}' from #{name}: #{error}")
|
|
|
|
errors.notFound(res, "Route #{req.path} not found.")
|