r3862 - in trunk/varnish-tools/webgui: . Varnish

petter at projects.linpro.no petter at projects.linpro.no
Tue Mar 3 10:47:49 CET 2009


Author: petter
Date: 2009-03-03 10:47:49 +0100 (Tue, 03 Mar 2009)
New Revision: 3862

Modified:
   trunk/varnish-tools/webgui/Varnish/RequestHandler.pm
   trunk/varnish-tools/webgui/Varnish/Util.pm
   trunk/varnish-tools/webgui/start.pl
Log:
Added the use of config file (at last),which is given as argument to web ui. If not given, it will check for /etc/varnish/webui.conf and then fall back to the default config.

Added a new config value, 'document_root', which is the root of the web server and contains the templates, images and CSS.


Modified: trunk/varnish-tools/webgui/Varnish/RequestHandler.pm
===================================================================
--- trunk/varnish-tools/webgui/Varnish/RequestHandler.pm	2009-03-03 09:33:27 UTC (rev 3861)
+++ trunk/varnish-tools/webgui/Varnish/RequestHandler.pm	2009-03-03 09:47:49 UTC (rev 3862)
@@ -135,10 +135,11 @@
 			defined($use_master_template) ? $use_master_template : 1;
 		
 		if ($content_template) {
+			my $document_root = get_config_value('document_root');
 			my %template_options = 
-				(die_on_bad_params => 0, global_vars => 1, loop_context_vars => 1);
+				(die_on_bad_params => 0, global_vars => 1, loop_context_vars => 1, path => [$document_root]);
 			if ($use_master_template) {
-				my $template_text = read_file("templates/master.tmpl");
+				my $template_text = read_file("$document_root/templates/master.tmpl");
 				$template_text =~ s/CONTENT_TEMPLATE/$content_template/;
 
 				my $template = HTML::Template->new_scalar_ref(	\$template_text,

Modified: trunk/varnish-tools/webgui/Varnish/Util.pm
===================================================================
--- trunk/varnish-tools/webgui/Varnish/Util.pm	2009-03-03 09:33:27 UTC (rev 3861)
+++ trunk/varnish-tools/webgui/Varnish/Util.pm	2009-03-03 09:47:49 UTC (rev 3862)
@@ -8,7 +8,9 @@
 use Algorithm::Diff;
 
 our @EXPORT = qw(
+				read_config
 				set_config
+				print_config
 				get_config_value
 				read_file
 				get_formatted_percentage
@@ -29,6 +31,25 @@
 	my $error;
 	my $log_handle;
 
+	sub read_config {
+		my ($filename) = @_;
+
+		my $handle;
+		if (!open($handle, "<$filename")) {
+			die "Could not open config file $filename\n";
+		}
+
+		while (<$handle>) {
+			if (/^(\w+)\s*=\s*(.*?)$/) {
+				my $key = lc $1;
+				my $value = $2;
+				$config{$key} = $value;
+			}
+		}
+
+		close($handle);	
+	}
+
 	sub set_config {
 		my ($config_ref) = @_;
 
@@ -44,6 +65,13 @@
 		Varnish::DB->init($config{'db_filename'});
 	}
 
+	sub print_config {
+		print "Config:\n";
+		while (my ($k, $v) = each(%config)) {
+			print "$k: $v\n";
+		}
+	}
+
 	sub get_config_value {
 		my ($key) = @_;
 

Modified: trunk/varnish-tools/webgui/start.pl
===================================================================
--- trunk/varnish-tools/webgui/start.pl	2009-03-03 09:33:27 UTC (rev 3861)
+++ trunk/varnish-tools/webgui/start.pl	2009-03-03 09:47:49 UTC (rev 3862)
@@ -14,9 +14,8 @@
 use Varnish::Statistics;
 use Varnish::DB;
 
-
-# Configuration starts here
-my %config = (
+my $global_config_filename = '/etc/varnish/webui.conf';
+my %default_config = (
 # 'address' is the IP to bind to. If not set, it listens on all.
 #	address				=> localhost,
 
@@ -39,14 +38,27 @@
 	large_graph_height	=> 500,
 
 # 'log_filename' is the filename to log errors and information about actions done in the GUI
-	log_filename		=> "varnish.log",
+	log_filename		=> 'varnish.log',
 
 # 'db_filename' is the sqlite3 database created with the SQL outputed from create_db_data.pl
 	db_filename			=> 'varnish.db',
+
+# 'document_root' is the root of the templates and css file
+	document_root		=> '.',
 );
-# End of configuration
 
-set_config(\%config);
+set_config(\%default_config);
+my $config_filename;
+if (@ARGV == 1 && -f $ARGV[0]) {
+	$config_filename = $ARGV[0];
+}
+elsif (-f $global_config_filename) {
+	$config_filename = $global_config_filename;
+}
+if ($config_filename) {
+	print "Using config file $config_filename\n";
+	read_config($config_filename);
+}
 
 # catch interupt to stop the daemon
 $SIG{'INT'} = sub {
@@ -59,8 +71,8 @@
 };
 
 log_info("Starting HTTP daemon");
-my $daemon = HTTP::Daemon->new(	LocalPort => $config{'port'}, 
-								LocalAddr => $config{'address'},
+my $daemon = HTTP::Daemon->new(	LocalPort => get_config_value('port'), 
+								LocalAddr => get_config_value('address'),
 								ReuseAddr => 1 );
 
 if (!$daemon) {
@@ -72,6 +84,7 @@
 my $running :shared;
 $running = 1;
 my $data_collector_handle = threads->create('data_collector_thread');
+my $document_root = get_config_value('document_root');
 while (my $connection = $daemon->accept) {
 	REQUEST:
 	while (my $request = $connection->get_request) {
@@ -81,7 +94,7 @@
 			$request->uri =~ m{/(.*?\.ico)}) {
 			my $filename = $1;
 			
-			$connection->send_file_response($filename);
+			$connection->send_file_response("$document_root/$filename");
 			next REQUEST;
 		}
 		elsif ($request->uri =~ m{/(.*?\.css)}) {
@@ -115,7 +128,7 @@
 
 sub data_collector_thread {
 	my $url = $daemon->url . "collect_data";
-	my $interval = $config{'poll_interval'};
+	my $interval = get_config_value('poll_interval');
 	
 	log_info("Data collector thread started. Polling URL $url at $interval seconds interval");
 	sleep 1; # wait for the server to come up



More information about the varnish-commit mailing list