2014-04-14 05:27:20 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
var restify = require('restify')
|
2015-05-13 08:00:51 +03:00
|
|
|
var bufferize = require('./lib/bufferize')
|
2014-07-17 21:29:36 +04:00
|
|
|
var version = require('./package.json').version
|
2015-07-03 02:39:13 +03:00
|
|
|
var errors = require('./lib/error')
|
2014-04-14 05:27:20 +04:00
|
|
|
|
2014-07-23 22:23:25 +04:00
|
|
|
function createServer(db) {
|
2014-04-14 05:27:20 +04:00
|
|
|
|
|
|
|
function reply(fn) {
|
|
|
|
return function (req, res, next) {
|
|
|
|
fn.call(db, req.params.id, req.body)
|
|
|
|
.then(
|
|
|
|
function (result) {
|
2014-07-17 21:29:36 +04:00
|
|
|
api.emit(
|
|
|
|
'success',
|
2014-04-16 22:16:19 +04:00
|
|
|
{
|
2015-02-02 09:12:47 +03:00
|
|
|
code: 200,
|
2014-04-16 22:16:19 +04:00
|
|
|
route: req.route.name,
|
2014-05-15 07:58:09 +04:00
|
|
|
method: req.method,
|
2015-02-02 09:12:47 +03:00
|
|
|
path: req.url,
|
2014-05-23 02:07:57 +04:00
|
|
|
t: Date.now() - req.time()
|
2014-04-16 22:16:19 +04:00
|
|
|
}
|
|
|
|
)
|
2014-04-14 05:27:20 +04:00
|
|
|
if (Array.isArray(result)) {
|
|
|
|
res.send(result.map(bufferize.unbuffer))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.send(bufferize.unbuffer(result || {}))
|
|
|
|
}
|
|
|
|
},
|
2015-07-03 02:39:13 +03:00
|
|
|
handleError.bind(null, req, res)
|
2014-04-14 05:27:20 +04:00
|
|
|
)
|
|
|
|
.done(next, next)
|
|
|
|
}
|
|
|
|
}
|
2015-07-03 02:39:13 +03:00
|
|
|
|
|
|
|
function handleError (req, res, err) {
|
|
|
|
if (typeof err !== 'object') {
|
|
|
|
err = { message: err || 'none' }
|
|
|
|
}
|
|
|
|
|
|
|
|
var statusCode = err.code || 500
|
|
|
|
|
|
|
|
api.emit(
|
|
|
|
'failure',
|
|
|
|
{
|
|
|
|
code: statusCode,
|
|
|
|
route: req.route ? req.route.name : 'unknown',
|
|
|
|
method: req.method,
|
|
|
|
path: req.url,
|
|
|
|
err: err,
|
|
|
|
t: Date.now() - req.time(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
res.send(statusCode, {
|
|
|
|
message: err.message,
|
|
|
|
errno: err.errno,
|
|
|
|
error: err.error,
|
|
|
|
code: err.code
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-04-14 05:27:20 +04:00
|
|
|
var api = restify.createServer()
|
|
|
|
api.use(restify.bodyParser())
|
|
|
|
api.use(bufferize.bufferizeRequest)
|
|
|
|
|
|
|
|
api.get('/account/:id', reply(db.account))
|
|
|
|
api.del('/account/:id', reply(db.deleteAccount))
|
|
|
|
api.put('/account/:id', reply(db.createAccount))
|
|
|
|
api.get('/account/:id/devices', reply(db.accountDevices))
|
2015-05-21 00:28:11 +03:00
|
|
|
api.post('/account/:id/checkPassword', reply(db.checkPassword))
|
2014-04-14 05:27:20 +04:00
|
|
|
api.post('/account/:id/reset', reply(db.resetAccount))
|
|
|
|
api.post('/account/:id/verifyEmail', reply(db.verifyEmail))
|
2014-07-17 21:29:36 +04:00
|
|
|
api.post('/account/:id/locale', reply(db.updateLocale))
|
2015-01-30 17:26:10 +03:00
|
|
|
api.post('/account/:id/lock', reply(db.lockAccount))
|
|
|
|
api.post('/account/:id/unlock', reply(db.unlockAccount))
|
|
|
|
api.get('/account/:id/unlockCode', reply(db.unlockCode))
|
2014-04-14 05:27:20 +04:00
|
|
|
|
|
|
|
api.get('/sessionToken/:id', reply(db.sessionToken))
|
|
|
|
api.del('/sessionToken/:id', reply(db.deleteSessionToken))
|
|
|
|
api.put('/sessionToken/:id', reply(db.createSessionToken))
|
|
|
|
|
|
|
|
api.get('/keyFetchToken/:id', reply(db.keyFetchToken))
|
|
|
|
api.del('/keyFetchToken/:id', reply(db.deleteKeyFetchToken))
|
|
|
|
api.put('/keyFetchToken/:id', reply(db.createKeyFetchToken))
|
|
|
|
|
|
|
|
api.get('/accountResetToken/:id', reply(db.accountResetToken))
|
|
|
|
api.del('/accountResetToken/:id', reply(db.deleteAccountResetToken))
|
|
|
|
api.put('/accountResetToken/:id', reply(db.createAccountResetToken))
|
|
|
|
|
|
|
|
api.get('/passwordChangeToken/:id', reply(db.passwordChangeToken))
|
|
|
|
api.del('/passwordChangeToken/:id', reply(db.deletePasswordChangeToken))
|
|
|
|
api.put('/passwordChangeToken/:id', reply(db.createPasswordChangeToken))
|
|
|
|
|
|
|
|
api.get('/passwordForgotToken/:id', reply(db.passwordForgotToken))
|
|
|
|
api.del('/passwordForgotToken/:id', reply(db.deletePasswordForgotToken))
|
|
|
|
api.put('/passwordForgotToken/:id', reply(db.createPasswordForgotToken))
|
|
|
|
api.post('/passwordForgotToken/:id/update', reply(db.updatePasswordForgotToken))
|
|
|
|
api.post('/passwordForgotToken/:id/verified', reply(db.forgotPasswordVerified))
|
|
|
|
|
|
|
|
api.get('/emailRecord/:id', reply(db.emailRecord))
|
|
|
|
api.head('/emailRecord/:id', reply(db.accountExists))
|
|
|
|
|
|
|
|
api.get('/__heartbeat__', reply(db.ping))
|
|
|
|
|
2014-04-16 22:16:19 +04:00
|
|
|
api.get(
|
|
|
|
'/',
|
|
|
|
function (req, res, next) {
|
2014-07-02 04:29:05 +04:00
|
|
|
res.send({ version: version })
|
2014-04-16 22:16:19 +04:00
|
|
|
next()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-03-13 04:57:36 +03:00
|
|
|
var memInterval = setInterval(function() {
|
|
|
|
api.emit('mem', process.memoryUsage())
|
|
|
|
}, 15000)
|
|
|
|
memInterval.unref()
|
|
|
|
|
2015-07-03 02:39:13 +03:00
|
|
|
api.on('NotFound', function (req, res) {
|
|
|
|
handleError(req, res, errors.notFound())
|
|
|
|
})
|
|
|
|
|
2014-07-02 04:29:05 +04:00
|
|
|
return api
|
|
|
|
}
|
2014-07-23 22:23:25 +04:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createServer: createServer,
|
2015-07-03 02:39:13 +03:00
|
|
|
errors: errors
|
2014-07-23 22:23:25 +04:00
|
|
|
}
|