implemented GET part of the tunnel API

This commit is contained in:
tigerbot 2017-05-30 12:15:19 -06:00
parent d4573994fc
commit 69d7d9e4b8
2 changed files with 30 additions and 1 deletions

View File

@ -130,5 +130,25 @@ module.exports.create = function (deps, config) {
return removeToken(data); return removeToken(data);
}); });
} }
, get: function (owner) {
return storage.all().then(function (tokens) {
var result = {};
tokens.forEach(function (data) {
if (!result[data.owner]) {
result[data.owner] = {};
}
if (!result[data.owner][data.tunnelUrl]) {
result[data.owner][data.tunnelUrl] = [];
}
data.decoded = JSON.parse(new Buffer(data.jwt.split('.')[0], 'base64'));
result[data.owner][data.tunnelUrl].push(data);
});
if (owner) {
return result[owner] || {};
}
return result;
});
}
}; };
}; };

View File

@ -118,6 +118,15 @@ module.exports.create = function (deps, conf) {
} }
, tunnel: function (req, res) { , tunnel: function (req, res) {
isAuthorized(req, res, function () { isAuthorized(req, res, function () {
if ('POST' !== req.method) {
res.setHeader('Content-Type', 'application/json');
return deps.tunneler.get(req.userId).then(function (result) {
res.end(JSON.stringify(result));
}, function (err) {
res.end(JSON.stringify({ error: { message: err.message, code: err.code, uri: err.uri } }));
});
}
jsonParser(req, res, function () { jsonParser(req, res, function () {
console.log('req.body', req.body); console.log('req.body', req.body);
@ -153,8 +162,8 @@ module.exports.create = function (deps, conf) {
}); });
} }
, request: function (req, res) { , request: function (req, res) {
jsonParser(req, res, function () {
isAuthorized(req, res, function () { isAuthorized(req, res, function () {
jsonParser(req, res, function () {
deps.request({ deps.request({
method: req.body.method || 'GET' method: req.body.method || 'GET'