This commit is contained in:
William Mantly 2019-12-22 14:42:35 -05:00
parent e81fd45373
commit fb71dc1dea
Signed by: wmantly
GPG Key ID: E1EEC7650BA97160
6 changed files with 35 additions and 8 deletions

View File

@ -2,31 +2,45 @@
const path = require('path'); const path = require('path');
const ejs = require('ejs') const ejs = require('ejs')
const express = require('express'); const express = require('express');
const app = express();
const middleware = require('./middleware/auth'); const middleware = require('./middleware/auth');
// Set up the express app.
const app = express();
// load the JSON parser middleware. Express will parse JSON into native objects
// for any request that has JSON in its content type.
app.use(express.json()); app.use(express.json());
// Set up the templating engine to build HTML for the front end.
app.set('views', path.join(__dirname, 'views')); app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs'); app.set('view engine', 'ejs');
// Have express server static content( images, CSS, browser JS) from the public
// local folder.
app.use('/static', express.static(path.join(__dirname, 'public'))) app.use('/static', express.static(path.join(__dirname, 'public')))
// Routes for front end content.
app.use('/', require('./routes/index')); app.use('/', require('./routes/index'));
// API routes for authentication.
app.use('/api/auth', require('./routes/auth')); app.use('/api/auth', require('./routes/auth'));
// API routes for working with users. All endpoints need to be have valid user.
app.use('/api/users', middleware.auth, require('./routes/users')); app.use('/api/users', middleware.auth, require('./routes/users'));
// API routes for working with hosts. All endpoints need to be have valid user.
app.use('/api/hosts', middleware.auth, require('./routes/hosts')); app.use('/api/hosts', middleware.auth, require('./routes/hosts'));
// catch 404 and forward to error handler // Catch 404 and forward to error handler. If none of the above routes are
// used, this is what will be called.
app.use(function(req, res, next) { app.use(function(req, res, next) {
var err = new Error('Not Found'); var err = new Error('Not Found');
err.status = 404; err.status = 404;
next(err); next(err);
}); });
// error handler // Error handler. This is where `next()` will go on error
app.use(function(err, req, res, next) { app.use(function(err, req, res, next) {
console.error(err.status || res.status, err.name, req.method, req.url); console.error(err.status || res.status, err.name, req.method, req.url);
console.error(err.message); console.error(err.message);
@ -37,4 +51,5 @@ app.use(function(err, req, res, next) {
res.json({name: err.name, message: err.message}); res.json({name: err.name, message: err.message});
}); });
// Allow the express app to be exported into other files.
module.exports = app; module.exports = app;

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const {promisify} = require('util'); const {promisify} = require('util');
const client = require('../redis'); const client = require('../utils/redis');
const {processKeys, ObjectValidateError} = require('../utils/object_validate'); const {processKeys, ObjectValidateError} = require('../utils/object_validate');
const hostKeysMap = { const hostKeysMap = {
@ -89,21 +89,33 @@ async function add(data, edit){
async function edit(data, host){ async function edit(data, host){
try{ try{
// Get the current host and trow a 404 if it doesnt exist.
let hostData = await getInfo({host}); let hostData = await getInfo({host});
// Check to see if host name changed
if(data.host && data.host !== host){ if(data.host && data.host !== host){
// Merge the current data into with the updated data
data = Object.assign({}, hostData, data); data = Object.assign({}, hostData, data);
if(await add('hosts', hostData)) await remove({host}); // Create a new record for the updated host. If that succeeds,
// delete the old recored
if(await add(hostData)) await remove({host});
}else{ }else{
// Update what ever fields that where passed.
// Validate the passed data, ignoring required fields.
data = processKeys(hostKeysMap, data, true); data = processKeys(hostKeysMap, data, true);
// console.log('host edit data', data);
// Loop over the data fields and apply them to redis
for(let key of Object.keys(data)){ for(let key of Object.keys(data)){
await client.HSET('host_' + host, key, data[key]); await client.HSET('host_' + host, key, data[key]);
} }
} }
} catch(error){ } catch(error){
// Pass any error to the calling function
throw error; throw error;
} }
} }

View File