This commit is contained in:
Josh Harvey 2016-11-27 18:12:49 +02:00
parent 3a99b7a6bc
commit 00d986376e
8 changed files with 149 additions and 155 deletions

View file

@ -22,20 +22,11 @@ const REQUEST_TTL = 3 * 60 * 1000
const pids = {}
const reboots = {}
function loadSettings (req, res, next) {
settingsLoader.settings()
.then(settings => {
req.settings = settings
next()
})
.catch(next)
}
function poll (req, res, next) {
const deviceId = req.deviceId
const deviceTime = req.deviceTime
const pid = req.query.pid
const settings = req.settings
const settings = settingsLoader.settings
const config = configManager.machineScoped(deviceId, settings.config)
pids[deviceId] = {pid, ts: Date.now()}
@ -84,13 +75,13 @@ function trade (req, res, next) {
const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
plugins.trade(req.settings, req.deviceId, tx)
plugins.trade(req.deviceId, tx)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
function stateChange (req, res, next) {
plugins.stateChange(req.settings, req.deviceId, req.deviceTime, req.body)
plugins.stateChange(req.deviceId, req.deviceTime, req.body)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
@ -99,7 +90,7 @@ function send (req, res, next) {
const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
return plugins.sendCoins(req.settings, req.deviceId, tx)
return plugins.sendCoins(req.deviceId, tx)
.then(status => {
const body = {txId: status && status.txId}
return cacheAndRespond(req, res, body)
@ -112,13 +103,13 @@ function cashOut (req, res, next) {
const tx = req.body
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
return plugins.cashOut(req.settings, req.deviceId, tx)
return plugins.cashOut(req.deviceId, tx)
.then(cryptoAddress => cacheAndRespond(req, res, {toAddress: cryptoAddress}))
.catch(next)
}
function dispenseAck (req, res, next) {
plugins.dispenseAck(req.settings, req.deviceId, req.body.tx)
plugins.dispenseAck(req.deviceId, req.body.tx)
.then(() => cacheAndRespond(req, res))
.catch(next)
}
@ -130,13 +121,13 @@ function deviceEvent (req, res, next) {
}
function verifyUser (req, res, next) {
plugins.verifyUser(req.settings, req.body)
plugins.verifyUser(req.body)
.then(idResult => cacheAndRespond(req, res, idResult))
.catch(next)
}
function verifyTx (req, res, next) {
plugins.verifyTransaction(req.settings, req.body)
plugins.verifyTransaction(req.body)
.then(idResult => cacheAndRespond(req, res, idResult))
.catch(next)
}
@ -164,7 +155,7 @@ function pair (req, res, next) {
function phoneCode (req, res, next) {
const phone = req.body.phone
return plugins.getPhoneCode(req.settings, phone)
return plugins.getPhoneCode(phone)
.then(code => cacheAndRespond(req, res, {code}))
.catch(err => {
if (err.name === 'BadNumberError') throw httpError('Bad number', 410)
@ -259,7 +250,7 @@ function errorHandler (err, req, res, next) {
const statusCode = err.code || 500
const json = {error: err.message}
logger.debug(err)
logger.error(err)
return updateCachedAction(req, json, statusCode)
.then(() => res.status(statusCode).json(json))
@ -334,19 +325,19 @@ function init (opts) {
app.post('/pair', pair)
app.get('/ca', ca)
app.get('/poll', authMiddleware, loadSettings, poll)
app.get('/poll', authMiddleware, poll)
app.post('/trade', authMiddleware, loadSettings, trade)
app.post('/send', authMiddleware, loadSettings, send)
app.post('/state', authMiddleware, loadSettings, stateChange)
app.post('/cash_out', authMiddleware, loadSettings, cashOut)
app.post('/dispense_ack', authMiddleware, loadSettings, dispenseAck)
app.post('/trade', authMiddleware, trade)
app.post('/send', authMiddleware, send)
app.post('/state', authMiddleware, stateChange)
app.post('/cash_out', authMiddleware, cashOut)
app.post('/dispense_ack', authMiddleware, dispenseAck)
app.post('/event', authMiddleware, deviceEvent)
app.post('/verify_user', authMiddleware, loadSettings, verifyUser)
app.post('/verify_transaction', authMiddleware, loadSettings, verifyTx)
app.post('/verify_user', authMiddleware, verifyUser)
app.post('/verify_transaction', authMiddleware, verifyTx)
app.post('/phone_code', authMiddleware, loadSettings, phoneCode)
app.post('/phone_code', authMiddleware, phoneCode)
app.post('/update_phone', authMiddleware, updatePhone)
app.get('/phone_tx', authMiddleware, fetchPhoneTx)
app.post('/register_redeem/:txId', authMiddleware, registerRedeem)
@ -373,13 +364,13 @@ function init (opts) {
res.sendStatus(200)
})
localApp.post('/dbChange', (req, res) => {
return configManager.load()
.then(config => {
return plugins.configure(config)
.then(() => logger.info('Config reloaded'))
localApp.post('/dbChange', (req, res, next) => {
return settingsLoader.load()
.then(() => logger.info('Config reloaded'))
.catch(err => {
logger.error(err)
res.sendStatus(500)
})
.catch(logger.error)
})
setInterval(pruneIdempotents, 60000)