added routes for hosts

This commit is contained in:
William Mantly 2019-12-12 13:33:54 -05:00
parent 7a05f6826f
commit 9bf224a8cb
Signed by: wmantly
GPG Key ID: E1EEC7650BA97160
3 changed files with 49 additions and 23 deletions

View File

@ -20,6 +20,22 @@ async function listAll(){
} }
async function listAllDetail(){
try{
let out = [];
let hosts = await listAll();
for(let host of hosts){
out.push(await getInfo({host}));
}
return out
}catch(error){
return new Error(error);
}
}
async function add(data){ async function add(data){
try{ try{
@ -53,4 +69,4 @@ async function remove(data){
} }
module.exports = {getInfo, listAll, add, remove}; module.exports = {getInfo, listAll, listAllDetail, add, remove};

View File

@ -17,8 +17,9 @@ router.get('/:host', async function(req, res){
router.get('/', async function(req, res){ router.get('/', async function(req, res){
try{ try{
let hosts = await Host.listAll(); return res.json({
return res.json({hosts: hosts}); hosts: req.query.detail ? await host.listAllDetail() : await Host.listAll()
});
}catch(error){ }catch(error){
return res.status(500).json({message: `ERROR ${error}`}); return res.status(500).json({message: `ERROR ${error}`});
} }
@ -36,10 +37,11 @@ router.post('/', async function(req, res){
} }
try{ try{
await Host.add({host, ip, targetPort, await Host.add({
username: req.user.username, host, ip, targetPort,
forceSSL: req.body.forceSSL, username: req.user.username,
targetSSL: req.body.targetSSL, forceSSL: req.body.forceSSL,
targetSSL: req.body.targetSSL,
}); });
return res.json({ return res.json({
@ -54,28 +56,21 @@ router.post('/', async function(req, res){
}); });
router.delete('/', async function(req, res){ router.delete('/:host', async function(req, res, next){
let host = req.body.host; let host = req.params.host;
let count;
if(!host){
return res.status(400).json({
message: `Missing fields: ${!host ? 'host' : ''}`
});
}
try{ try{
count = await Host.remove({host}); let count = await Host.remove({host});
return res.json({
message: `Host ${host} deleted`,
});
}catch(error){ }catch(error){
return res.status(500).json({ return res.status(500).json({
message: `ERROR: ${error}` message: `ERROR: ${error}`
}); });
} }
return res.json({
message: `Host ${host} deleted`,
});
}); });
module.exports = router; module.exports = router;

View File

@ -2,8 +2,23 @@ var express = require('express');
var router = express.Router(); var router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { router.get('/', async function(req, res, next) {
res.render('index', { title: 'Express' }); res.render('hosts', { title: 'Express' });
});
/* GET home page. */
router.get('/hosts', function(req, res, next) {
res.render('hosts', { title: 'Express' });
});
/* GET home page. */
router.get('/users', function(req, res, next) {
res.render('users', { title: 'Express' });
});
/* GET home page. */
router.get('/login', function(req, res, next) {
res.render('login', {redirect: req.query.redirect});
}); });
module.exports = router; module.exports = router;