Commits

Jarrod Funnell committed ffea8a8

AJAX makes everything shinier. Oh and wtf since when were we using YAML and not YAML::XS? Shameful.

  • Participants
  • Parent commits 3bee7ba

Comments (0)

Files changed (12)

File conf/Example.yml

 exit:
   Lock: "Off"
   Channel: "No Channels"
+

File conf/dummy.yml

 id: asdf01
 module: Device::Test
 interface: 'None'
-active: 1
+active: 1
+

File lib/ModHandler.pm

 use Mouse;
 use AnyEvent::Filesys::Notify;
 use Module::Load;
-use YAML qw(LoadFile DumpFile);
+use YAML::XS qw(LoadFile DumpFile);
 
 has 'outpipe' => (
 	is       => 'ro',

File webmin/lib/Webmin.pm

 sub startup {
 	my $self = shift;
 	
-	# $self->plugin('yaml_config', {
-	# 	file      => 'webmin.conf.yaml',
-	# 	stash_key => 'conf',
-	# 	class     => 'YAML::XS'
-	# });
+	$self->plugin('JSONConfig', {
+		file      => 'webmin.conf.json',
+	});
 	
 
 	# Router
 	my $r = $self->routes;
 
 	# Normal route to controller
-	$r->route('/')->to('status#overview');
+	$r->route('/')->to('root#index');
 
-	$r->route('/:device')->to('status#overview');
+	$r->route('/ajax/:action')->to(controller => 'ajax');
 }
 
 1;

File webmin/lib/Webmin/Ajax.pm

+package Webmin::Ajax;
+use Mojo::Base 'Mojolicious::Controller';
+
+use File::Spec;
+use Proc::Daemon;
+use Try::Tiny;
+
+sub start_coeus {
+	my $self = shift;
+
+	try {
+		my $pid = Proc::Daemon->new(
+			work_dir     => $self->config->{coeus_path},
+			pid_file     => 'coeus.pid',
+			exec_command => File::Spec->catfile($self->config->{coeus_path}, 'Coeus.pl'),
+		)->Init();
+
+		if ($pid){
+			$self->render(
+				text => 'success',
+			);
+		}
+		else {
+			$self->render(
+				text => 'No PID. He\'s dead Jim',
+			);
+		}
+	}
+	catch {
+		$self->render(
+			text => $!,
+		);
+	}
+}
+
+1;

File webmin/lib/Webmin/Root.pm

+package Webmin::Root;
+use Mojo::Base 'Mojolicious::Controller';
+
+use YAML::XS qw/LoadFile DumpFile/;
+use File::Spec;
+use File::Glob 'bsd_glob';
+
+sub index {
+	my $self = shift;
+	
+	state $mod_conf_dir = File::Spec->catfile($self->config->{coeus_path}, 'conf');
+	state $last_modified = 0;
+	state $conf_files = {};
+	state $conf_contents = {};
+
+	my $pattern = File::Spec->catfile($mod_conf_dir, '*.{yml,yaml,conf}');
+	for my $filename ( bsd_glob($pattern) ){
+		#Get the files' "last modified" time.
+		my $modified = (stat($filename))[9];
+		
+		#Load the yaml file if needed.
+		if (!$conf_files->{$filename} || $conf_files->{$filename} != $modified){
+			$conf_files->{$filename} = $modified;
+			my $yml = LoadFile($filename);
+			$conf_contents->{$filename} = $yml;
+		}
+	}
+
+	$self->render(
+		confs   => $conf_contents
+	);
+
+}
+
+1;

File webmin/lib/Webmin/Status.pm

-package Webmin::Status;
-use Mojo::Base 'Mojolicious::Controller';
-
-use YAML::XS qw/LoadFile DumpFile/;
-use File::Spec;
-use File::Glob 'bsd_glob';
-# This action will render a template
-
-no warnings 'once';
-
-sub overview {
-	my $self = shift;
-	
-	state $mod_conf_dir = File::Spec->catfile($global::dir, '..', 'conf');
-
-	my $confs = {};
-
-	my $pattern = File::Spec->catfile($mod_conf_dir, '*.{yml,yaml,conf}');
-	for my $conf ( bsd_glob($pattern) ){
-		my $yml = LoadFile($conf);
-		$confs->{$yml->{id}} = $yml;
-	}
-
-	$self->render(
-		message => 'Welcome to the Coeus Webmin!',
-		confs    => $confs,
-	);
-
-}
-
-1;

File webmin/public/js/functions.js

+"use strict";
+
+//Get a JSON websocket going (? maybe?)
+
+
+
+//Enable tooltips
+$(".has-tooltip-load").tooltip({title:'Click to load'});
+$(".has-tooltip-unload").tooltip({title:'Click to unload'});
+
+//Hook up buttons
+$('#start-coeus').click(function(){
+	if ($(this).hasClass('disabled'))
+		return;
+
+	$(this).button('loading');
+
+	var request = $.ajax('/ajax/start_coeus/');
+
+	request.always(function(){
+		$('#start-coeus').button('reset');
+	});
+
+	request.done(function(ret){
+		if (ret == 'success'){
+			$('#start-coeus').addClass('disabled');
+			$('#stop-coeus').removeClass('disabled');
+		}
+		else {
+			alert( "Could not start Coeus: " + ret );
+		}
+
+	});
+	request.fail(function(jqXHR, textStatus) {
+ 		alert( "Request failed: " + textStatus );
+	});
+});
+
+$('#stop-coeus').click(function(){
+	if ($(this).hasClass('disabled'))
+		return;
+
+	$(this).button('loading');
+
+
+});
+

File webmin/templates/layouts/default.html.ep

 	<head>
 		<title><%= title %></title>
 		<meta name="viewport" content="width=device-width, initial-scale=1.0">
-		<meta name="description" content="">
-		<meta name="author" content="">
+		<meta name="description" content="A webmin for coeus-server">
+		<meta name="author" content="Jarrod Funnell">
+		<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
 		<link href="/css/bootstrap.min.css" rel="stylesheet">
 		<style>
 			body { padding-top:5px; }
 	</div>
 	
 	<script src="/js/bootstrap.min.js"></script>
-	<script type="text/javascript">
-		$(".has-tooltip").tooltip();
-	</script>
-
+	<script src="/js/functions.js"></script>
 	</body>
 </html>

File webmin/templates/root/index.html.ep

+% layout 'default';
+% title 'Webmin';
+
+<div class="hero-unit">
+	<h1>Coeus-Webmin.</h1>
+	<p class="lead">Coeus is currently <%= "stopped." %></p>
+	<span class="pull-right">
+		<a id="start-coeus" class="btn btn-primary" data-loading-text="Starting..." >Start Server</a> 
+		<a id="stop-coeus" class="btn btn-inverse disabled" data-loading-text="Stopping...">Stop Server</a>
+	</span>
+</div>
+
+% my $ff = 0;
+% for my $device (values %$confs) {
+	% $ff = !$ff;
+	%== '<div class="row-fluid">' if ($ff)
+	<div class="span6 alert <%== "alert-success" if $device->{active} %>">
+		<div class="page-header pagination-centered">
+			<h2><%= $device->{name} %></h2>
+			<h2>
+				<small>
+					ID: <%= $device->{id} %>
+				</small>
+				<br>
+				<small>
+					Status: 
+				</small>
+				<a 
+					id="<%= $device->{id} %>-toggle-active" 
+					class="btn btn-inverse has-tooltip-<%==$device->{active} ? "unload" : "load"%>"
+					href="#"
+				>
+					<%= $device->{active} ? "Loaded" : "Unloaded" %> 
+				</a>
+			</h2>
+		</div>
+
+		<div class="form-horizontal">
+		<div class="control-group">
+			<label class="control-label" for="<%== $device->{id}."-modinput" %>">
+				Module
+			</label>
+			<div class="controls">
+				<input id="<%== $device->{id}."-modinput" %>" type="text" value="<%== $device->{module} %>">
+			</div>
+		</div>
+		<div class="control-group">
+			<label class="control-label" for="<%== $device->{id}."-interface-input" %>">
+				Interface
+			</label>
+			
+			<div class="controls">
+				<input id="<%== $device->{id}."-interface-input" %>" type="text" value="<%== $device->{interface} %>">
+			</div>
+		</div>
+		</div>
+	</div>
+	%== '</div>' if (!$ff);
+% }
+
+<div>
+	<a class="btn btn-primary btn-large" href="#">Add New Device</a>
+</div>

File webmin/templates/status/overview.html.ep

-% layout 'default';
-% title 'Webmin';
-
-<div class="hero-unit">
-	<h1>Coeus-Webmin.</h1>
-	<p class="lead">Coeus is currently <%= "stopped." %></p>
-	<span class="pull-right">
-		<a id="start-coeus" class="btn btn-primary">Start Server</a> 
-		<a id="stop-coeus" class="btn btn-inverse disabled">Stop Server</a>
-	</span>
-</div>
-
-% my $ff = 0;
-% for my $device (values %$confs) {
-	% $ff = !$ff;
-	%== '<div class="row-fluid">' if ($ff)
-	<div class="span6 alert <%== "alert-success" if $device->{active} %>">
-		<div class="page-header pagination-centered">
-			<h2><%= $device->{name} %></h2>
-			<h2>
-				<small>
-					ID: <%= $device->{id} %>
-				</small>
-				<br>
-				<small>
-					Status: 
-				</small>
-				<a 
-					id="<%= $device->{id} %>-toggle-active" 
-					class="btn btn-inverse has-tooltip"
-					href="#"
-					data-original-title="<%= $device->{active} ? "Click to unload" : "Click to load" %>"
-				>
-					<%= $device->{active} ? "Loaded" : "Unloaded" %> 
-				</a>
-			</h2>
-		</div>
-
-		<div class="form-horizontal">
-		<div class="control-group">
-			<label class="control-label" for="<%== $device->{id}."-modinput" %>">
-				Module
-			</label>
-			<div class="controls">
-				<input id="<%== $device->{id}."-modinput" %>" type="text" value="<%== $device->{module} %>">
-			</div>
-		</div>
-		<div class="control-group">
-			<label class="control-label" for="<%== $device->{id}."-interface-input" %>">
-				Interface
-			</label>
-			
-			<div class="controls">
-				<input id="<%== $device->{id}."-interface-input" %>" type="text" value="<%== $device->{interface} %>">
-			</div>
-		</div>
-		</div>
-	</div>
-	%== '</div>' if (!$ff);
-% }
-
-<div>
-	<a class="btn btn-primary btn-large" href="#">Add New Device</a>
-</div>

File webmin/webmin.conf.json

+{
+	"coeus_path": "/home/timbus/Projects/Coeus-Server"
+}