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

View File

@ -2,8 +2,23 @@ var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
router.get('/', async function(req, res, next) {
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;