Feat: refactor routes.js express entrypoint config
Feat: express config script refactor Feat: add state and settingsCache files
This commit is contained in:
parent
c3f8f98c26
commit
85235eaa13
22 changed files with 807 additions and 1 deletions
35
lib/routes/logsRoutes.js
Normal file
35
lib/routes/logsRoutes.js
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
const express = require('express')
|
||||
const router = express.Router()
|
||||
const _ = require('lodash/fp')
|
||||
|
||||
const state = require('../middlewares/state')
|
||||
const logs = require('../logs')
|
||||
|
||||
const THROTTLE_LOGS_QUERY = 30 * 1000
|
||||
|
||||
function getLastSeen (req, res, next) {
|
||||
const deviceId = req.deviceId
|
||||
const timestamp = Date.now()
|
||||
const shouldTrigger = !state.canGetLastSeenMap[deviceId] ||
|
||||
timestamp - state.canGetLastSeenMap[deviceId] >= THROTTLE_LOGS_QUERY
|
||||
|
||||
if (shouldTrigger) {
|
||||
state.canGetLastSeenMap[deviceId] = timestamp
|
||||
return logs.getLastSeen(deviceId)
|
||||
.then(r => res.json(r))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
return res.status(408).json({})
|
||||
}
|
||||
|
||||
function updateLogs (req, res, next) {
|
||||
return logs.update(req.deviceId, req.body.logs)
|
||||
.then(status => res.json({ success: status }))
|
||||
.catch(next)
|
||||
}
|
||||
|
||||
router.get('/', getLastSeen)
|
||||
router.post('/', updateLogs)
|
||||
|
||||
module.exports = router
|
||||
Loading…
Add table
Add a link
Reference in a new issue