diff --git a/nodejs/models/hosts.js b/nodejs/models/hosts.js index 3e1e6c3..7876107 100755 --- a/nodejs/models/hosts.js +++ b/nodejs/models/hosts.js @@ -5,6 +5,7 @@ const client = require('../redis'); async function getInfo(data){ let info = await client.HGETALL('host_' + data.host); + info['host'] = data.host; return info } @@ -21,40 +22,31 @@ async function listAll(){ async function listAllDetail(){ - try{ - let out = []; - let hosts = await listAll(); + let out = []; - for(let host of hosts){ - out.push(await getInfo({host})); - } - - return out - }catch(error){ - return new Error(error); + for(let host of await listAll()){ + out.push(await getInfo({host})); } + + return out } async function add(data){ - try{ - await client.SADD('hosts', data.host); - await client.HSET('host_' + data.host, 'ip', data.ip); - await client.HSET('host_' + data.host, 'updated', (new Date).getTime()); - await client.HSET('host_' + data.host, 'username', data.username); - await client.HSET('host_' + data.host, 'targetPort', data.targetPort); - if(data.forceSSL !== undefined){ - await client.HSET('host_' + data.host, 'forcessl', !!data.forceSSL); - } - if(data.targetSSL !== undefined){ - await client.HSET('host_' + data.host, 'targetssl', !!data.targetSSL); - } - } catch (error){ - - return new Error(error); + await client.SADD('hosts', data.host); + await client.HSET('host_' + data.host, 'ip', data.ip); + await client.HSET('host_' + data.host, 'updated', (new Date).getTime()); + await client.HSET('host_' + data.host, 'username', data.username); + await client.HSET('host_' + data.host, 'targetPort', data.targetPort); + if(data.forceSSL !== undefined){ + await client.HSET('host_' + data.host, 'forcessl', !!data.forceSSL); } + if(data.targetSSL !== undefined){ + await client.HSET('host_' + data.host, 'targetssl', !!data.targetSSL); + } + } diff --git a/nodejs/routes/hosts.js b/nodejs/routes/hosts.js index ab05f2b..f3c4893 100755 --- a/nodejs/routes/hosts.js +++ b/nodejs/routes/hosts.js @@ -15,13 +15,14 @@ router.get('/:host', async function(req, res){ }); }); -router.get('/', async function(req, res){ +router.get('/', async function(req, res, next){ try{ return res.json({ - hosts: req.query.detail ? await host.listAllDetail() : await Host.listAll() + hosts: req.query.detail ? await Host.listAllDetail() : await Host.listAll() }); }catch(error){ - return res.status(500).json({message: `ERROR ${error}`}); + res.status(500) + next(error) } });