fixes
This commit is contained in:
parent
e81fd45373
commit
fb71dc1dea
@ -2,31 +2,45 @@
|
||||
|
||||
const path = require('path');
|
||||
const ejs = require('ejs')
|
||||
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
|
||||
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());
|
||||
|
||||
// Set up the templating engine to build HTML for the front end.
|
||||
app.set('views', path.join(__dirname, 'views'));
|
||||
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')))
|
||||
|
||||
// Routes for front end content.
|
||||
app.use('/', require('./routes/index'));
|
||||
|
||||
// API routes for authentication.
|
||||
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'));
|
||||
|
||||
// API routes for working with hosts. All endpoints need to be have valid user.
|
||||
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) {
|
||||
var err = new Error('Not Found');
|
||||
err.status = 404;
|
||||
next(err);
|
||||
});
|
||||
|
||||
// error handler
|
||||
// Error handler. This is where `next()` will go on error
|
||||
app.use(function(err, req, res, next) {
|
||||
console.error(err.status || res.status, err.name, req.method, req.url);
|
||||
console.error(err.message);
|
||||
@ -37,4 +51,5 @@ app.use(function(err, req, res, next) {
|
||||
res.json({name: err.name, message: err.message});
|
||||
});
|
||||
|
||||
// Allow the express app to be exported into other files.
|
||||
module.exports = app;
|
||||
|
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const {promisify} = require('util');
|
||||
const client = require('../redis');
|
||||
const client = require('../utils/redis');
|
||||
const {processKeys, ObjectValidateError} = require('../utils/object_validate');
|
||||
|
||||
const hostKeysMap = {
|
||||
@ -89,21 +89,33 @@ async function add(data, edit){
|
||||
|
||||
async function edit(data, host){
|
||||
try{
|
||||
|
||||
// Get the current host and trow a 404 if it doesnt exist.
|
||||
let hostData = await getInfo({host});
|
||||
|
||||
// Check to see if host name changed
|
||||
if(data.host && data.host !== host){
|
||||
|
||||
// Merge the current data into with the updated 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{
|
||||
// Update what ever fields that where passed.
|
||||
|
||||
// Validate the passed data, ignoring required fields.
|
||||
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)){
|
||||
await client.HSET('host_' + host, key, data[key]);
|
||||
}
|
||||
}
|
||||
} catch(error){
|
||||
// Pass any error to the calling function
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user