WIP
This commit is contained in:
parent
becd62a1fb
commit
938eb9ac97
9 changed files with 153 additions and 68 deletions
|
|
@ -1,5 +1,6 @@
|
|||
'use strict'
|
||||
|
||||
const morgan = require('morgan')
|
||||
const helmet = require('helmet')
|
||||
const bodyParser = require('body-parser')
|
||||
const BigNumber = require('bignumber.js')
|
||||
|
|
@ -76,20 +77,13 @@ function poll (req, res) {
|
|||
|
||||
const config = plugins.getConfig(deviceId)
|
||||
|
||||
console.log('DEBUG30')
|
||||
|
||||
plugins.pollQueries(deviceId)
|
||||
.then(results => {
|
||||
console.log('DEBUG31')
|
||||
|
||||
const cartridges = results.cartridges
|
||||
|
||||
const reboot = pid && reboots[deviceId] && reboots[deviceId] === pid
|
||||
console.log('DEBUG2: %j, %s, %s', reboots, reboot, pid)
|
||||
const langs = config.languages.machineLanguages
|
||||
|
||||
console.log('DEBUG33.1')
|
||||
|
||||
const locale = {
|
||||
currency: config.currencies.fiatCurrency,
|
||||
localeInfo: {
|
||||
|
|
@ -126,7 +120,6 @@ function poll (req, res) {
|
|||
}
|
||||
|
||||
function trade (req, res, next) {
|
||||
console.log('DEBUG24')
|
||||
const tx = req.body
|
||||
tx.cryptoAtoms = new BigNumber(tx.cryptoAtoms)
|
||||
|
||||
|
|
@ -183,22 +176,20 @@ function ca (req, res) {
|
|||
const token = req.query.token
|
||||
|
||||
return pairing.authorizeCaDownload(token)
|
||||
.then(valid => {
|
||||
if (valid) return res.json({ca: pairing.ca()})
|
||||
|
||||
return res.status(408).end()
|
||||
})
|
||||
.then(ca => res.json({ca}))
|
||||
.catch(() => res.status(408).end())
|
||||
}
|
||||
|
||||
function pair (req, res) {
|
||||
function pair (req, res, next) {
|
||||
const token = req.query.token
|
||||
const deviceId = getDeviceId(req)
|
||||
|
||||
return pairing.pair(token, deviceId)
|
||||
.then(valid => {
|
||||
if (valid) return cacheAndRespond(req, res)
|
||||
if (valid) return res.end()
|
||||
throw httpError('Pairing failed')
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function phoneCode (req, res) {
|
||||
|
|
@ -233,20 +224,17 @@ function registerRedeem (req, res) {
|
|||
.then(() => cacheAndRespond(req, res))
|
||||
}
|
||||
|
||||
function waitForDispense (req, res) {
|
||||
function waitForDispense (req, res, next) {
|
||||
logger.debug('waitForDispense')
|
||||
return plugins.fetchTx(req.params.txId)
|
||||
.then(tx => {
|
||||
logger.debug('tx fetched')
|
||||
logger.debug(tx)
|
||||
if (!tx) return res.sendStatus(404)
|
||||
if (tx.status === req.query.status) return res.sendStatus(304)
|
||||
res.json({tx})
|
||||
})
|
||||
.catch(err => {
|
||||
logger.error(err)
|
||||
res.sendStatus(500)
|
||||
})
|
||||
.then(tx => {
|
||||
logger.debug('tx fetched')
|
||||
logger.debug(tx)
|
||||
if (!tx) return res.sendStatus(404)
|
||||
if (tx.status === req.query.status) return res.sendStatus(304)
|
||||
res.json({tx})
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
function dispense (req, res) {
|
||||
|
|
@ -261,12 +249,12 @@ function isUniqueViolation (err) {
|
|||
}
|
||||
|
||||
function cacheAction (req, res, next) {
|
||||
console.log('DEBUG22: %s', req.path)
|
||||
const requestId = req.headers['request-id']
|
||||
if (!requestId) return next()
|
||||
|
||||
const sql = `insert into idempotents (request_id, device_id, body, status, pending)
|
||||
values ($1, $2, $3, $4, $5)`
|
||||
|
||||
const requestId = req.headers['request-id']
|
||||
const deviceId = getDeviceId(req)
|
||||
|
||||
db.none(sql, [requestId, deviceId, {}, 204, true])
|
||||
|
|
@ -284,21 +272,25 @@ function cacheAction (req, res, next) {
|
|||
}
|
||||
|
||||
function updateCachedAction (req, body, status) {
|
||||
const requestId = req.headers['request-id']
|
||||
if (!requestId) return Promise.resolve()
|
||||
|
||||
const sql = `update idempotents set body=$1, status=$2, pending=$3
|
||||
where request_id=$4 and device_id=$5 and pending=$6`
|
||||
|
||||
const requestId = req.headers['request-id']
|
||||
const deviceId = getDeviceId(req)
|
||||
|
||||
return db.none(sql, [body, status, false, requestId, deviceId, true])
|
||||
}
|
||||
|
||||
function postErrorHandler (err, req, res, next) {
|
||||
function errorHandler (err, req, res, next) {
|
||||
const statusCode = err.code || 500
|
||||
const json = {error: err.message}
|
||||
|
||||
logger.debug(err)
|
||||
|
||||
return updateCachedAction(req, json, statusCode)
|
||||
.then(() => res.status(statusCode).json({}))
|
||||
.then(() => res.status(statusCode).json(json))
|
||||
}
|
||||
|
||||
function cacheAndRespond (req, res, _body, _status) {
|
||||
|
|
@ -361,6 +353,7 @@ function init (opts) {
|
|||
? (req, res, next) => next()
|
||||
: authorize
|
||||
|
||||
app.use(morgan('dev'))
|
||||
app.use(helmet())
|
||||
app.use(bodyParser.json())
|
||||
app.use(filterOldRequests)
|
||||
|
|
@ -388,7 +381,7 @@ function init (opts) {
|
|||
app.get('/await_dispense/:txId', authMiddleware, waitForDispense)
|
||||
app.post('/dispense', authMiddleware, dispense)
|
||||
|
||||
app.post('*', postErrorHandler)
|
||||
app.use('*', errorHandler)
|
||||
|
||||
localApp.get('/pid', (req, res) => {
|
||||
const deviceId = req.query.device_id
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue