Merge branch 'handler-restructure'

This commit is contained in:
Rob 2015-11-12 17:34:03 -05:00
commit 62cd00e328
2 changed files with 63 additions and 31 deletions

View file

@ -32,6 +32,22 @@ module.exports.handlers =
'trial_request': 'trial_requests/trial_request_handler'
'user_polls_record': 'polls/user_polls_record_handler'
module.exports.handlerUrlOverrides =
'analytics_log_event': 'analytics_log_event'
'analytics_perday': 'analytics.perday'
'analytics_string': 'analytics.string'
'analytics_stripe_invoice': 'analytics.stripe.invoice'
'level_component': 'level.component'
'level_feedback': 'level.feedback'
'level_session': 'level.session'
'level_system': 'level.system'
'thang_type': 'thang.type'
'thang_component': 'thang.component'
'user_code_problem': 'user.code.problem'
'user_remark': 'user.remark'
'mail_sent': 'mail.sent'
'user_polls_record': 'user.polls.record'
module.exports.routes =
[
'routes/admin'

View file

@ -1,6 +1,7 @@
log = require 'winston'
errors = require '../commons/errors'
handlers = require('../commons/mapping').handlers
handlerUrlOverrides = require('../commons/mapping').handlerUrlOverrides
mongoose = require 'mongoose'
hipchat = require '../hipchat'
@ -19,40 +20,55 @@ module.exports.setup = (app) ->
res.send docs
res.end
app.all '/db/*', (req, res) ->
bindHandler = (name, module) ->
routeHandler = (req, res) ->
res.setHeader('Content-Type', 'application/json')
parts = req.path[4..].split('/')
if (not req.user) and req.route.method isnt 'get'
return errors.unauthorized(res, 'Must have an identity to do anything with the db. Do you have cookies enabled?')
try
handler = require('../' + name)
return handler.getLatestVersion(req, res, parts[1], parts[3]) if parts[2] is 'version'
return handler.versions(req, res, parts[1]) if parts[2] is 'versions'
return handler.files(req, res, parts[1]) if parts[2] is 'files'
return handler.getNamesByIDs(req, res) if req.route.method in ['get', 'post'] and parts[1] is 'names'
return handler.getByRelationship(req, res, parts[1..]...) if parts.length > 2
return handler.getById(req, res, parts[1]) if req.route.method is 'get' and parts[1]?
return handler.patch(req, res, parts[1]) if req.route.method is 'patch' and parts[1]?
handler[req.route.method](req, res, parts[1..]...)
catch error
errorMessage = "Error trying db method #{req?.route?.method} route #{parts} from #{name}: #{error}"
if req.user?
userInfo = req.user.getUserInfo()
errorMessage += "\n-- User Info Id: #{userInfo.id} #{if req.user.isAnonymous() then '' else 'Email:'} #{userInfo.email}"
log.error(errorMessage)
log.error(error)
log.error(error.stack)
# TODO: Generally ignore this error: error: Error trying db method get route analytics.log.event from undefined: Error: Cannot find module '../undefined'
unless "#{parts}" in ['analytics.users.active']
hipchat.sendHipChatMessage errorMessage, ['tower'], papertrail: true
errors.notFound(res, "Route #{req?.path} not found.")
app.all '/db/' + moduleName + '/*', routeHandler
app.all '/db/' + moduleName, routeHandler
app.get '/db/:module/schema', (req, res) ->
res.setHeader('Content-Type', 'application/json')
module = req.path[4..]
getSchema req, res, req.params.module
parts = module.split('/')
module = parts[0]
return getSchema(req, res, module) if parts[1] is 'schema'
if (not req.user) and req.route.method isnt 'get'
return errors.unauthorized(res, 'Must have an identity to do anything with the db. Do you have cookies enabled?')
for moduleNameUnder of handlers
name = handlers[moduleNameUnder]
moduleName = handlerUrlOverrides[moduleNameUnder] or moduleNameUnder
bindHandler name, moduleName
try
moduleName = module.replace new RegExp('\\.', 'g'), '_'
name = handlers[moduleName]
handler = require('../' + name)
return handler.getLatestVersion(req, res, parts[1], parts[3]) if parts[2] is 'version'
return handler.versions(req, res, parts[1]) if parts[2] is 'versions'
return handler.files(req, res, parts[1]) if parts[2] is 'files'
return handler.getNamesByIDs(req, res) if req.route.method in ['get', 'post'] and parts[1] is 'names'
return handler.getByRelationship(req, res, parts[1..]...) if parts.length > 2
return handler.getById(req, res, parts[1]) if req.route.method is 'get' and parts[1]?
return handler.patch(req, res, parts[1]) if req.route.method is 'patch' and parts[1]?
handler[req.route.method](req, res, parts[1..]...)
catch error
errorMessage = "Error trying db method #{req?.route?.method} route #{parts} from #{name}: #{error}"
if req.user?
userInfo = req.user.getUserInfo()
errorMessage += "\n-- User Info Id: #{userInfo.id} #{if req.user.isAnonymous() then '' else 'Email:'} #{userInfo.email}"
log.error(errorMessage)
log.error(error)
log.error(error.stack)
# TODO: Generally ignore this error: error: Error trying db method get route analytics.log.event from undefined: Error: Cannot find module '../undefined'
unless "#{parts}" in ['analytics.users.active']
hipchat.sendHipChatMessage errorMessage, ['tower'], papertrail: true
errors.notFound(res, "Route #{req?.path} not found.")
# Fall back for URLs that start with /db/ but dont map to a handler.
app.all '/db/*', (req, res) ->
res.setHeader('Content-Type', 'text/plain')
errors.notFound(res, "Route #{req?.path} not found.")
getSchema = (req, res, moduleName) ->
try