diff --git a/nodejs/LICENSE b/LICENSE similarity index 100% rename from nodejs/LICENSE rename to LICENSE diff --git a/nodejs/README.md b/README.md similarity index 100% rename from nodejs/README.md rename to README.md diff --git a/nodejs/app.js b/nodejs/app.js index 750be23..bfa0767 100755 --- a/nodejs/app.js +++ b/nodejs/app.js @@ -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; diff --git a/nodejs/models/hosts.js b/nodejs/models/hosts.js index 0f95fac..12d7af4 100755 --- a/nodejs/models/hosts.js +++ b/nodejs/models/hosts.js @@ -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; } } diff --git a/nodejs/ssh-keygen b/nodejs/ssh-keygen deleted file mode 100755 index e69de29..0000000 diff --git a/nodejs/redis.js b/nodejs/utils/redis.js similarity index 100% rename from nodejs/redis.js rename to nodejs/utils/redis.js