Commit 08bc1e1c by zCaesar

can sub group

parent f1ffcccf
var config = require('config');
// https://github.com/isaacs/node-lru-cache
var LRU = require("lru-cache") ,
var LRU = require("lru-cache"),
cache = LRU({
max: 500,
maxAge: 1000 * 60 * 5
......@@ -12,13 +13,13 @@ function authCheck(client_id, token, topic, callback) {
callback(true);
}
module.exports = function(options={}) {
module.exports = function (options = {}) {
debug = options.debug || false;
return function(req, res, next) {
return function (req, res, next) {
if (debug) {
if (options.debug) {
console.log('auth_on_subscribe-------------');
console.log(req.body);
}
......@@ -27,24 +28,35 @@ module.exports = function(options={}) {
var authstatus;
var topic = req.body.topics[0].topic;
var cachekey = 'sub:'+req.body.client_id +':'+ req.body.username+':'+topic;
var cachekey = 'sub:' + req.body.client_id + ':' + req.body.username + ':' + topic;
var authstatus = cache.get(cachekey);
if (typeof(authstatus)=='undefined') {
// Set Response
var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
// console.log(_id)
var _ftopic = require('./utils/getTopic').getTopic(topic, _id) // get topic where concat with groupID
var _topic = [{ // setTopic for response
'topic': _ftopic,
'qos': 0
}]
var response = {
'result': 'ok',
'topics': _topic
}
if (typeof (authstatus) == 'undefined') {
// cache missed
authCheck(req.body.client_id, req.body.username, topic, function(result) {
authCheck(req.body.client_id, req.body.username, topic, function (result) {
cache.set(cachekey, result);
res.send({
"result": result?"ok":"no"
});
res.send(response);
});
}
else {
res.send({
"result": authstatus?"ok":"no"
});
res.send(response);
}
next();
})
}
else {
res.send({
......@@ -54,3 +66,4 @@ module.exports = function(options={}) {
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment