2014-02-04 17:08:20 -05:00
|
|
|
log = require 'winston'
|
2014-01-22 17:57:41 -05:00
|
|
|
errors = require '../commons/errors'
|
2014-02-04 16:29:13 -05:00
|
|
|
handlers = require('../commons/mapping').handlers
|
|
|
|
schemas = require('../commons/mapping').schemas
|
2014-01-03 13:32:13 -05:00
|
|
|
|
2014-02-04 16:29:13 -05:00
|
|
|
module.exports.setup = (app) ->
|
2014-01-03 13:32:13 -05:00
|
|
|
app.all '/db/*', (req, res) ->
|
|
|
|
res.setHeader('Content-Type', 'application/json')
|
|
|
|
module = req.path[4..]
|
|
|
|
|
|
|
|
parts = module.split('/')
|
|
|
|
module = parts[0]
|
|
|
|
return getSchema(req, res, module) if parts[1] is 'schema'
|
2014-02-24 23:27:38 -05:00
|
|
|
return errors.unauthorized(res, 'Must have an identity to do anything with the db.') unless req.user
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
try
|
2014-01-22 17:57:41 -05:00
|
|
|
moduleName = module.replace '.', '_'
|
|
|
|
name = handlers[moduleName]
|
2014-02-04 17:08:20 -05:00
|
|
|
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.search(req, res) if req.route.method is 'get' and parts[1] is 'search'
|
|
|
|
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)
|
2014-01-03 13:32:13 -05:00
|
|
|
catch error
|
2014-02-04 17:08:20 -05:00
|
|
|
log.error("Error trying db method #{req.route.method} route #{parts} from #{name}: #{error}")
|
|
|
|
log.error(error)
|
2014-01-14 17:13:47 -05:00
|
|
|
errors.notFound(res, "Route #{req.path} not found.")
|
2014-01-03 13:32:13 -05:00
|
|
|
|
|
|
|
getSchema = (req, res, moduleName) ->
|
|
|
|
try
|
2014-01-23 17:06:12 -05:00
|
|
|
name = schemas[moduleName.replace '.', '_']
|
2014-02-04 16:29:13 -05:00
|
|
|
schema = require('../' + name)
|
2014-01-23 17:06:12 -05:00
|
|
|
|
2014-01-03 13:32:13 -05:00
|
|
|
res.send(schema)
|
|
|
|
res.end()
|
|
|
|
|
|
|
|
catch error
|
2014-02-04 17:08:20 -05:00
|
|
|
log.error("Error trying to grab schema from #{name}: #{error}")
|
2014-01-14 17:13:47 -05:00
|
|
|
errors.notFound(res, "Schema #{moduleName} not found.")
|