Last active
March 30, 2020 11:51
-
-
Save harlesbayu/023d297aadd3b85783c522e64e5291e9 to your computer and use it in GitHub Desktop.
Rsmq management api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const RedisSMQ = require('rsmq'); | |
const config = require('../../../config'); | |
const rsmq = new RedisSMQ({ | |
url: config.get('REDIS_URL'), | |
ns: 'rsmq' | |
}); | |
const controllerCreateQueue = (req, res) => { | |
try { | |
return rsmq.createQueue({ | |
qname: req.body.queueName, | |
maxsize: -1 | |
}, (err, resp) => { | |
if (err) { | |
return res.json(err); | |
} | |
return res.json(resp); | |
}); | |
} catch (error) { | |
return res.json(error); | |
} | |
}; | |
const controllerGetQueue = (req, res) => { | |
try { | |
return rsmq.listQueues((err, queues) => { | |
if (err) { | |
return res.json(err); | |
} | |
return res.json({ | |
message: queues | |
}); | |
}); | |
} catch (error) { | |
return res.json(error); | |
} | |
}; | |
const controllerDeleteQueue = (req, res) => { | |
try { | |
return rsmq.deleteQueue({ | |
qname: req.body.queueName | |
}, (err, resp) => { | |
if (err) { | |
return res.json(err); | |
} | |
if (resp === 1) { | |
return res.json('Queue and all messages deleted.'); | |
} | |
return res.json('Queue not found.'); | |
}); | |
} catch (error) { | |
return res.json(error); | |
} | |
}; | |
const controllerSendQueue = (req, res) => { | |
try { | |
const data = req.body; | |
return rsmq.sendMessage({ | |
qname: req.query.queueName, | |
message: JSON.stringify(data) | |
}, (err, resp) => { | |
if (err) { | |
return res.json(err); | |
} | |
return res.json(resp); | |
}); | |
} catch (error) { | |
return res.json(error); | |
} | |
}; | |
const controllerReceiveQueue = (req, res) => { | |
try { | |
return rsmq.receiveMessage({ | |
qname: req.body.queueName | |
}, (err, resp) => { | |
if (err) { | |
return res.json(err); | |
} | |
if (resp.id) { | |
return res.json({ | |
resp, | |
data: JSON.stringify(resp.message) | |
}); | |
} | |
return res.json(err); | |
}); | |
} catch (error) { | |
return res.json(error); | |
} | |
}; | |
module.exports = (router) => { | |
router.get('/get-receive', controllerReceiveQueue); | |
router.get('/get-queue', controllerGetQueue); | |
router.post('/', controllerSendQueue); | |
router.post('/create-queue', controllerCreateQueue); | |
router.delete('/delete', controllerDeleteQueue); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment