host detail fixes

This commit is contained in:
William Mantly 2019-12-20 13:46:05 -05:00
parent a9026f037e
commit 408c86f86a
Signed by: wmantly
GPG Key ID: E1EEC7650BA97160
2 changed files with 21 additions and 28 deletions

View File

@ -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);
}
}

View File

@ -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)
}
});