From 143936ab5da3b0911b492d544279a07c03781ce5 Mon Sep 17 00:00:00 2001 From: Akkadius Date: Sat, 2 Sep 2017 01:04:47 -0500 Subject: [PATCH] Base config - API - few others --- api/get_subnet_details.js | 23 +++++++++++ app.js | 11 +++++- config/glass_config.json | 6 +++ lib/render_template.js | 7 ++++ public/assets/js/api-examples.js | 24 +++++++++++ public/assets/js/api_examples.js | 14 ------- public/assets/js/glass-core.js | 59 ++++++++++++++++++++++++++++ public/assets/js/pjax.js | 4 +- public/templates/api_examples.html | 1 - public/templates/glass_settings.html | 14 +++++++ public/templates/index.html | 55 ++++++++++++-------------- routes/api_examples.js | 27 ++++++++++--- routes/dashboard.js | 7 ++-- routes/glass_settings.js | 39 ++++++++++++++++++ routes/glass_settings_save.js | 19 +++++++++ 15 files changed, 252 insertions(+), 58 deletions(-) create mode 100644 api/get_subnet_details.js create mode 100644 config/glass_config.json create mode 100644 public/assets/js/api-examples.js delete mode 100644 public/assets/js/api_examples.js create mode 100644 public/assets/js/glass-core.js create mode 100644 public/templates/glass_settings.html create mode 100644 routes/glass_settings.js create mode 100644 routes/glass_settings_save.js diff --git a/api/get_subnet_details.js b/api/get_subnet_details.js new file mode 100644 index 0000000..38b0764 --- /dev/null +++ b/api/get_subnet_details.js @@ -0,0 +1,23 @@ +/** + * Created by cmiles on 8/9/2017. + */ + +var express = require('express'); +var router = express.Router(); + +router.get('/', function(req, res, next) { + + var json_file = require('jsonfile'); + glass_config = json_file.readFileSync('config/glass_config.json'); + + res.setHeader('Content-Type', 'application/json'); + + const execSync = require('child_process').execSync; + output = execSync('./bin/dhcpd-pools -c ' + glass_config.config_file + ' -l ' + glass_config.leases_file + ' -f j -A -s e'); + + console.log(JSON.parse(output)); + + res.send(JSON.stringify(JSON.parse(output))); +}); + +module.exports = router; \ No newline at end of file diff --git a/app.js b/app.js index eab65cd..42dc590 100644 --- a/app.js +++ b/app.js @@ -7,6 +7,10 @@ var bodyParser = require('body-parser'); var app = express(); +/* Read Config */ +var json_file = require('jsonfile'); +glass_config = json_file.readFileSync('config/glass_config.json'); + // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); @@ -22,9 +26,12 @@ app.use('/get_dashboard', require('./routes/dashboard')); app.use('/get_stats', require('./routes/get_stats')); app.use('/dhcp_leases', require('./routes/dhcp_leases')); app.use('/api_examples', require('./routes/api_examples')); +app.use('/glass_settings', require('./routes/glass_settings')); +app.use('/glass_settings_save', require('./routes/glass_settings_save')); /* API Routes */ app.use('/api/get_active_leases/', require('./api/get_active_leases')); +app.use('/api/get_subnet_details/', require('./api/get_subnet_details')); app.set('view engine', 'html'); @@ -62,7 +69,7 @@ dhcp_lease_data = {}; lease_read_buffer = ""; fs = require('fs'); -fs.readFile('/var/lib/dhcp/dhcpd.leases', 'utf8', function (err,data) { +fs.readFile(glass_config.leases_file, 'utf8', function (err,data) { if (err) { return console.log(err); } @@ -73,7 +80,7 @@ fs.readFile('/var/lib/dhcp/dhcpd.leases', 'utf8', function (err,data) { var tail_module = require('always-tail'); tail = new tail_module( - "/var/lib/dhcp/dhcpd.leases", + glass_config.leases_file, "\n", options ); diff --git a/config/glass_config.json b/config/glass_config.json new file mode 100644 index 0000000..49932b2 --- /dev/null +++ b/config/glass_config.json @@ -0,0 +1,6 @@ +{ + "admin_user": "glassadmin", + "admin_password": "glassadmin", + "leases_file": "/var/lib/dhcp/dhcpd.leases", + "config_file": "/etc/dhcp/dhcpd.conf" +} diff --git a/lib/render_template.js b/lib/render_template.js index 7da7111..bba535a 100644 --- a/lib/render_template.js +++ b/lib/render_template.js @@ -23,4 +23,11 @@ module.exports = { set_template_variable: function(template, variable, value) { return template.replace("[" + variable + "]", value); }, + form_body: function (id, inputs) { + return '
' + inputs + '
'; + }, + form_input: function (title, input) { + return '
' + input + '
'; + }, }; + diff --git a/public/assets/js/api-examples.js b/public/assets/js/api-examples.js new file mode 100644 index 0000000..fc8051e --- /dev/null +++ b/public/assets/js/api-examples.js @@ -0,0 +1,24 @@ +/** + * Created by cmiles on 8/9/2017. + */ + +function api_example(example) { + switch(example) { + case "get_active_leases": + $.getJSON( "/api/get_active_leases", function( data ) { + $("#get_active_leases").html('
' + JSON.stringify(data, null, 2) + '
').fadeOut(100).fadeIn(100); + }); + break; + case "get_subnet_details": + $.getJSON( "/api/get_subnet_details", function( data ) { + $("#get_subnet_details").html('
' + JSON.stringify(data, null, 2) + '
').fadeOut(100).fadeIn(100); + }); + break; + default: + return; + } +} + +function clear_api(example) { + $("#" + example).html(''); +} \ No newline at end of file diff --git a/public/assets/js/api_examples.js b/public/assets/js/api_examples.js deleted file mode 100644 index 1a1ae5e..0000000 --- a/public/assets/js/api_examples.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * Created by cmiles on 8/9/2017. - */ - -function api_example(example) { - switch(example) { - case "get_active_leases": - $.getJSON( "/api/get_active_leases", function( data ) { - $("#get-active-leases").html('
' + JSON.stringify(data, null, 2) + '
').fadeOut(100).fadeIn(100); - }); - default: - return; - } -} \ No newline at end of file diff --git a/public/assets/js/glass-core.js b/public/assets/js/glass-core.js new file mode 100644 index 0000000..85b6a57 --- /dev/null +++ b/public/assets/js/glass-core.js @@ -0,0 +1,59 @@ +/** + * Created by cmiles on 8/9/2017. + */ + +function get_form_query_string(form_id){ + query_string = ""; + $('#' + form_id).find('input, select, textarea').each(function (key) { + val = $(this).val(); + if (val == 'undefined') { + val = ''; + } + if($(this).attr('type') == "checkbox"){ + if (!$(this).is(':checked')) { + val = 0; + } + } + query_string = query_string + "&" + $(this).attr('id') + "=" + encodeURIComponent(val); + }); + return query_string; +} + +function save_config() { + glass_settings = get_form_query_string("glass-settings-form"); + + $.post( "/glass_settings_save", glass_settings, function( data ) { + $( "#glass_settings_result" ).html( data ); + }); +} + +function notification(text){ + colorName = 'bg-black'; + animateEnter = 'animated fadeInDown'; + animateExit = 'animated fadeOutUp'; + var allowDismiss = true; + + $.notify({ + message: text + }, + { + type: colorName, + allow_dismiss: allowDismiss, + newest_on_top: true, + timer: 1000, + animate: { + enter: animateEnter, + exit: animateExit + }, + template: '' + }); +} \ No newline at end of file diff --git a/public/assets/js/pjax.js b/public/assets/js/pjax.js index 07455e5..0bb19c2 100644 --- a/public/assets/js/pjax.js +++ b/public/assets/js/pjax.js @@ -1,5 +1,6 @@ /** * Created by cmiles on 3/13/2015. + * Custom pjax framework used for years */ var pjax_request_count = 1; var pjax_debug = 1; @@ -90,9 +91,6 @@ function do_pjax_request(request_url){ $('#body-content').css("opacity", "1"); $('#body-content').html(e_res); - /* Set new page scroll position to 0 */ - // document.body.scrollTop = document.documentElement.scrollTop = 0; - // $(scroll_target).scrollTop(0); $(scroll_target).animate({scrollTop: 0}, 100); last_navigated_url = request_url; diff --git a/public/templates/api_examples.html b/public/templates/api_examples.html index fa7a663..2eba945 100644 --- a/public/templates/api_examples.html +++ b/public/templates/api_examples.html @@ -4,7 +4,6 @@

[title] - See below for details

diff --git a/public/templates/glass_settings.html b/public/templates/glass_settings.html new file mode 100644 index 0000000..36385bb --- /dev/null +++ b/public/templates/glass_settings.html @@ -0,0 +1,14 @@ +
+
+
+
+

+ Glass Settings +

+
+
+ [body_content] +
+
+
+
\ No newline at end of file diff --git a/public/templates/index.html b/public/templates/index.html index e795914..eac283c 100644 --- a/public/templates/index.html +++ b/public/templates/index.html @@ -70,7 +70,7 @@ @@ -156,6 +149,9 @@ + + + @@ -167,7 +163,8 @@ - + + diff --git a/routes/api_examples.js b/routes/api_examples.js index 847b2ae..63b9cbc 100644 --- a/routes/api_examples.js +++ b/routes/api_examples.js @@ -8,18 +8,33 @@ var template_render = require('../lib/render_template.js'); router.get('/', function(req, res, next) { - api_examples = template_render.get_template("api_examples"); - api_examples = template_render.set_template_variable(api_examples, "title", "Get Active Leases"); + /* Get Active Leases */ + get_active_leases = template_render.get_template("api_examples"); + get_active_leases = template_render.set_template_variable(get_active_leases, "title", "Get Active Leases"); example_body = 'URL: /api/get_active_leases

'; - example_body = example_body + '

'; - example_body = example_body + '
'; + example_body = example_body + ' '; + example_body = example_body + '

'; + example_body = example_body + '
'; - api_examples = template_render.set_template_variable(api_examples, "example_body", example_body); + get_active_leases = template_render.set_template_variable(get_active_leases, "example_body", example_body); + + /* Get Subnet Details */ + get_subnet_details = template_render.get_template("api_examples"); + get_subnet_details = template_render.set_template_variable(get_subnet_details, "title", "Get Subnet Details"); + + example_body = 'URL: /api/get_subnet_details

'; + example_body = example_body + ' '; + example_body = example_body + '

'; + example_body = example_body + '
'; + + get_subnet_details = template_render.set_template_variable(get_subnet_details, "example_body", example_body); res.send ( template_render.get_index_template( - api_examples, + get_active_leases + + get_subnet_details + , req.url ) ); diff --git a/routes/dashboard.js b/routes/dashboard.js index e3f4942..10b1f4e 100644 --- a/routes/dashboard.js +++ b/routes/dashboard.js @@ -3,12 +3,13 @@ var router = express.Router(); var fs = require('fs'); var template_render = require('../lib/render_template.js'); -/* GET home page. */ +var json_file = require('jsonfile'); +glass_config = json_file.readFileSync('config/glass_config.json'); + router.get('/', function(req, res, next) { const execSync = require('child_process').execSync; - output = execSync('/home/cmiles/dhcpd-pools -c /home/cmiles/dhcpd.conf -l /home/cmiles/dhcpd.leases -f J -A -s e'); - // output = execSync('/home/cmiles/dhcpd-pools -c /etc/dhcp/dhcpd.conf -l /var/lib/dhcp/dhcpd.leases -f j -A -s e'); + output = execSync('./bin/dhcpd-pools -c ' + glass_config.config_file + ' -l ' + glass_config.leases_file + ' -f j -A -s e'); var dhcp_data = JSON.parse(output); diff --git a/routes/glass_settings.js b/routes/glass_settings.js new file mode 100644 index 0000000..8985633 --- /dev/null +++ b/routes/glass_settings.js @@ -0,0 +1,39 @@ +/** + * Created by cmiles on 8/9/2017. + */ + +var express = require('express'); +var router = express.Router(); +var fs = require('fs'); +var template_render = require('../lib/render_template.js'); + +router.get('/', function(req, res, next) { + + glass_settings_template = template_render.get_template("glass_settings"); + + var json_file = require('jsonfile'); + + /* Read Config */ + glass_config = json_file.readFileSync('config/glass_config.json'); + + /* Leases File */ + input = template_render.form_input('Leases File', ''); + + /* Config File */ + input = input + template_render.form_input('Config File', ''); + + /* Admin User */ + input = input + template_render.form_input('Admin User', ''); + input = input + template_render.form_input('Admin Password', ''); + + input = input + '
'; + input = input + '
'; + + form_data = template_render.form_body("glass-settings-form", input); + + glass_settings_template = template_render.set_template_variable(glass_settings_template, "body_content", form_data); + + res.send(template_render.get_index_template(glass_settings_template, req.url)); +}); + +module.exports = router; \ No newline at end of file diff --git a/routes/glass_settings_save.js b/routes/glass_settings_save.js new file mode 100644 index 0000000..c79a303 --- /dev/null +++ b/routes/glass_settings_save.js @@ -0,0 +1,19 @@ +/** + * Created by cmiles on 8/9/2017. + */ + +var express = require('express'); +var router = express.Router(); + +router.post('/', function(req, res, next) { + var request = req.body; + var json_file = require('jsonfile'); + + json_file.writeFile('./config/glass_config.json', request, {spaces: 2}, function(err) { + console.error(err) + }); + + res.send(''); +}); + +module.exports = router; \ No newline at end of file