Base config - API - few others

This commit is contained in:
Akkadius 2017-09-02 01:04:47 -05:00
parent 9aa32c8aa3
commit 143936ab5d
15 changed files with 252 additions and 58 deletions

23
api/get_subnet_details.js Normal file
View File

@ -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;

11
app.js
View File

@ -7,6 +7,10 @@ var bodyParser = require('body-parser');
var app = express(); 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 // uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev')); 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('/get_stats', require('./routes/get_stats'));
app.use('/dhcp_leases', require('./routes/dhcp_leases')); app.use('/dhcp_leases', require('./routes/dhcp_leases'));
app.use('/api_examples', require('./routes/api_examples')); 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 */ /* API Routes */
app.use('/api/get_active_leases/', require('./api/get_active_leases')); 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'); app.set('view engine', 'html');
@ -62,7 +69,7 @@ dhcp_lease_data = {};
lease_read_buffer = ""; lease_read_buffer = "";
fs = require('fs'); 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) { if (err) {
return console.log(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'); var tail_module = require('always-tail');
tail = new tail_module( tail = new tail_module(
"/var/lib/dhcp/dhcpd.leases", glass_config.leases_file,
"\n", "\n",
options options
); );

6
config/glass_config.json Normal file
View File

@ -0,0 +1,6 @@
{
"admin_user": "glassadmin",
"admin_password": "glassadmin",
"leases_file": "/var/lib/dhcp/dhcpd.leases",
"config_file": "/etc/dhcp/dhcpd.conf"
}

View File

@ -23,4 +23,11 @@ module.exports = {
set_template_variable: function(template, variable, value) { set_template_variable: function(template, variable, value) {
return template.replace("[" + variable + "]", value); return template.replace("[" + variable + "]", value);
}, },
form_body: function (id, inputs) {
return '<div class="form-group" id="' + id + '">' + inputs + '</div>';
},
form_input: function (title, input) {
return '<label>' + title + '</label><div class="form-group"><div class="form-line">' + input + '</div></div>';
},
}; };

View File

@ -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('<pre>' + JSON.stringify(data, null, 2) + '</pre>').fadeOut(100).fadeIn(100);
});
break;
case "get_subnet_details":
$.getJSON( "/api/get_subnet_details", function( data ) {
$("#get_subnet_details").html('<pre>' + JSON.stringify(data, null, 2) + '</pre>').fadeOut(100).fadeIn(100);
});
break;
default:
return;
}
}
function clear_api(example) {
$("#" + example).html('');
}

View File

@ -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('<pre>' + JSON.stringify(data, null, 2) + '</pre>').fadeOut(100).fadeIn(100);
});
default:
return;
}
}

View File

@ -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: '<div data-notify="container" class="bootstrap-notify-container alert alert-dismissible {0} ' + (allowDismiss ? "p-r-35" : "") + '" role="alert">' +
'<button type="button" aria-hidden="true" class="close" data-notify="dismiss">×</button>' +
'<span data-notify="icon"></span> ' +
'<span data-notify="title">{1}</span> ' +
'<span data-notify="message">{2}</span>' +
'<div class="progress" data-notify="progressbar">' +
'<div class="progress-bar progress-bar-{0}" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"></div>' +
'</div>' +
'<a href="{3}" target="{4}" data-notify="url"></a>' +
'</div>'
});
}

View File

@ -1,5 +1,6 @@
/** /**
* Created by cmiles on 3/13/2015. * Created by cmiles on 3/13/2015.
* Custom pjax framework used for years
*/ */
var pjax_request_count = 1; var pjax_request_count = 1;
var pjax_debug = 1; var pjax_debug = 1;
@ -90,9 +91,6 @@ function do_pjax_request(request_url){
$('#body-content').css("opacity", "1"); $('#body-content').css("opacity", "1");
$('#body-content').html(e_res); $('#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); $(scroll_target).animate({scrollTop: 0}, 100);
last_navigated_url = request_url; last_navigated_url = request_url;

View File

@ -4,7 +4,6 @@
<div class="header"> <div class="header">
<h2> <h2>
[title] [title]
<small>See below for details</small>
</h2> </h2>
</div> </div>
<div class="body"> <div class="body">

View File

@ -0,0 +1,14 @@
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="card">
<div class="header">
<h2>
Glass Settings
</h2>
</div>
<div class="body">
[body_content]
</div>
</div>
</div>
</div>

View File

@ -70,7 +70,7 @@
<!-- Menu --> <!-- Menu -->
<div class="menu"> <div class="menu">
<ul class="list"> <ul class="list">
<li class="header active">MAIN NAVIGATION</li> <li class="header">Glass Navigation</li>
<li class="active"> <li class="active">
<a href="/" pjax="1"> <a href="/" pjax="1">
<i class="material-icons">home</i> <i class="material-icons">home</i>
@ -83,48 +83,41 @@
<span>Leases</span> <span>Leases</span>
</a> </a>
</li> </li>
<li>
<a href="/dhcp_logs" pjax="1">
<i class="material-icons">view_stream</i>
<span>Logs</span>
</a>
</li>
<li class="header">Settings</li>
<li>
<a href="/glass_settings" pjax="1">
<i class="material-icons">settings</i>
<span>Glass Settings</span>
</a>
</li>
<li class="header">Glass API</li>
<li> <li>
<a href="/api_examples" pjax="1"> <a href="/api_examples" pjax="1">
<i class="material-icons">widgets</i> <i class="material-icons">widgets</i>
<span>API Examples</span> <span>API Examples</span>
</a> </a>
</li> </li>
<li>
<a href="../../pages/helper-classes.html">
<i class="material-icons">layers</i>
<span>Helper Classes</span>
</a>
</li>
<li class="header">LABELS</li>
<li>
<a href="javascript:void(0);">
<i class="material-icons col-red">donut_large</i>
<span>Important</span>
</a>
</li>
<li>
<a href="javascript:void(0);">
<i class="material-icons col-amber">donut_large</i>
<span>Warning</span>
</a>
</li>
<li>
<a href="javascript:void(0);">
<i class="material-icons col-light-blue">donut_large</i>
<span>Information</span>
</a>
</li>
</ul> </ul>
</div> </div>
<!-- #Menu --> <!-- #Menu -->
<!-- Footer --> <!-- Footer -->
<div class="legal"> <div class="legal">
<div class="copyright"> <div class="copyright">
&copy; 2016 - 2017 <a href="javascript:void(0);">AdminBSB - Material Design</a>. &copy; 2017 <a href="javascript:void(0);">Glass - Chris Miles</a><br>
&copy; 2016 - 2017 <a href="javascript:void(0);">AdminBSB - Material Design</a>
</div> </div>
<div class="version"> <div class="version">
<b>Version: </b> 1.0.5 <b>Version: </b> 1.0.0
</div> </div>
</div> </div>
<!-- #Footer --> <!-- #Footer -->
@ -156,6 +149,9 @@
<!-- Slimscroll Plugin Js --> <!-- Slimscroll Plugin Js -->
<script src="assets/plugins/jquery-slimscroll/jquery.slimscroll.js"></script> <script src="assets/plugins/jquery-slimscroll/jquery.slimscroll.js"></script>
<!-- Notify plugin -->
<script src="assets/plugins/bootstrap-notify/bootstrap-notify.js"></script>
<!-- Waves Effect Plugin Js --> <!-- Waves Effect Plugin Js -->
<script src="assets/plugins/node-waves/waves.js"></script> <script src="assets/plugins/node-waves/waves.js"></script>
@ -167,7 +163,8 @@
<script src="assets/js/pjax.js"></script> <script src="assets/js/pjax.js"></script>
<script src="assets/js/api_examples.js"></script> <script src="assets/js/api-examples.js"></script>
<script src="assets/js/glass-core.js"></script>
<script src="assets/plugins/jquery-datatable/jquery.dataTables.js"></script> <script src="assets/plugins/jquery-datatable/jquery.dataTables.js"></script>
<script src="assets/plugins/jquery-datatable/skin/bootstrap/js/dataTables.bootstrap.js"></script> <script src="assets/plugins/jquery-datatable/skin/bootstrap/js/dataTables.bootstrap.js"></script>

View File

@ -8,18 +8,33 @@ var template_render = require('../lib/render_template.js');
router.get('/', function(req, res, next) { router.get('/', function(req, res, next) {
api_examples = template_render.get_template("api_examples"); /* Get Active Leases */
api_examples = template_render.set_template_variable(api_examples, "title", "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: <code>/api/get_active_leases</code><br><br>'; example_body = 'URL: <code>/api/get_active_leases</code><br><br>';
example_body = example_body + '<button type="button" onclick="api_example(\'get_active_leases\')" class="btn btn-default waves-effect">Try It!</button><br><br>'; example_body = example_body + '<button type="button" onclick="api_example(\'get_active_leases\')" class="btn btn-default waves-effect">Try It!</button> ';
example_body = example_body + '<div id="get-active-leases"></div>'; example_body = example_body + '<button type="button" onclick="clear_api(\'get_active_leases\')" class="btn btn-default waves-effect">Clear</button><br><br>';
example_body = example_body + '<div id="get_active_leases"></div>';
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: <code>/api/get_subnet_details</code><br><br>';
example_body = example_body + '<button type="button" onclick="api_example(\'get_subnet_details\')" class="btn btn-default waves-effect">Try It!</button> ';
example_body = example_body + '<button type="button" onclick="clear_api(\'get_subnet_details\')" class="btn btn-default waves-effect">Clear</button><br><br>';
example_body = example_body + '<div id="get_subnet_details"></div>';
get_subnet_details = template_render.set_template_variable(get_subnet_details, "example_body", example_body);
res.send ( res.send (
template_render.get_index_template( template_render.get_index_template(
api_examples, get_active_leases +
get_subnet_details
,
req.url req.url
) )
); );

View File

@ -3,12 +3,13 @@ var router = express.Router();
var fs = require('fs'); var fs = require('fs');
var template_render = require('../lib/render_template.js'); 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) { router.get('/', function(req, res, next) {
const execSync = require('child_process').execSync; 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('./bin/dhcpd-pools -c ' + glass_config.config_file + ' -l ' + glass_config.leases_file + ' -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');
var dhcp_data = JSON.parse(output); var dhcp_data = JSON.parse(output);

39
routes/glass_settings.js Normal file
View File

@ -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', '<input type="input" class="form-control" id="leases_file" placeholder="/var/lib/dhcp/dhcpd.leases" value="' + glass_config.leases_file + '">');
/* Config File */
input = input + template_render.form_input('Config File', '<input type="input" class="form-control" id="config_file" placeholder="/etc/dhcp/dhcpd.conf" value="' + glass_config.config_file + '">');
/* Admin User */
input = input + template_render.form_input('Admin User', '<input type="input" class="form-control" id="admin_user" placeholder="Username" value="' + glass_config.admin_user + '">');
input = input + template_render.form_input('Admin Password', '<input type="input" class="form-control" id="admin_password" placeholder="Password" value="' + glass_config.admin_password + '">');
input = input + '<br><button type="button" class="btn btn-info waves-effect" onclick="save_config()"><i class="material-icons">settings</i> <span>Save Config</span></button>';
input = input + '<br><div id="glass_settings_result"></div>';
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;

View File

@ -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('<script type="text/javascript">notification(\'Saved Config!\')</script>');
});
module.exports = router;