From 525467563130ffa1804217c0252f0bff25cac61d Mon Sep 17 00:00:00 2001 From: petter Date: Tue, 3 Mar 2009 09:47:49 +0000 Subject: [PATCH] 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. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3862 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- .../webgui/Varnish/RequestHandler.pm | 5 +-- varnish-tools/webgui/Varnish/Util.pm | 28 ++++++++++++++++ varnish-tools/webgui/start.pl | 33 +++++++++++++------ 3 files changed, 54 insertions(+), 12 deletions(-) diff --git a/varnish-tools/webgui/Varnish/RequestHandler.pm b/varnish-tools/webgui/Varnish/RequestHandler.pm index 87d40cd6..7bd4d99d 100644 --- a/varnish-tools/webgui/Varnish/RequestHandler.pm +++ b/varnish-tools/webgui/Varnish/RequestHandler.pm @@ -135,10 +135,11 @@ use Socket; 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, diff --git a/varnish-tools/webgui/Varnish/Util.pm b/varnish-tools/webgui/Varnish/Util.pm index 198abec5..9941d1dc 100644 --- a/varnish-tools/webgui/Varnish/Util.pm +++ b/varnish-tools/webgui/Varnish/Util.pm @@ -8,7 +8,9 @@ use URI::Escape; use Algorithm::Diff; our @EXPORT = qw( + read_config set_config + print_config get_config_value read_file get_formatted_percentage @@ -29,6 +31,25 @@ our @EXPORT = qw( 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 @@ our @EXPORT = qw( 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) = @_; diff --git a/varnish-tools/webgui/start.pl b/varnish-tools/webgui/start.pl index b48c9409..671f556f 100755 --- a/varnish-tools/webgui/start.pl +++ b/varnish-tools/webgui/start.pl @@ -14,9 +14,8 @@ use Varnish::Node; 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 @@ my %config = ( 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 @@ $SIG{'PIPE'} = sub { }; 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 @@ print "Web server started with URL: " . $daemon->url, "\n"; 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 @@ while (my $connection = $daemon->accept) { $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 @@ $data_collector_handle->join(); 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 -- 2.39.5