From 9ca8c76cd6d2db1f6da1f6f6bcd828f7f01a9897 Mon Sep 17 00:00:00 2001 From: Steven Schubiger Date: Sun, 1 Sep 2013 18:37:24 +0200 Subject: [PATCH] Move config data to files --- client.conf | 10 ++++++++++ client.pl | 51 +++++++++++++++++++++++++++------------------------ dynuser.conf | 3 --- server.cgi | 35 ++++++++++++++++++++++++----------- server.conf | 3 +++ 5 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 client.conf delete mode 100644 dynuser.conf create mode 100644 server.conf diff --git a/client.conf b/client.conf new file mode 100644 index 0000000..354a64f --- /dev/null +++ b/client.conf @@ -0,0 +1,10 @@ +[path] +hosts_file = hosts +session_file = session.dat + +[url] +server_url = http://refcnt.org/~sts/cgi-bin/ketterle/server.cgi + +[data] +netz = netz1 +name = name1 diff --git a/client.pl b/client.pl index 917eec3..1d03898 100755 --- a/client.pl +++ b/client.pl @@ -24,27 +24,17 @@ use constant false => 0; use Config::Tiny; use Digest::MD5 qw(md5_hex); use Fcntl ':flock'; -use File::Spec::Functions qw(rel2abs); +use File::Spec::Functions qw(catfile rel2abs); +use FindBin qw($Bin); use Getopt::Long qw(:config no_auto_abbrev no_ignore_case); use JSON qw(decode_json); use LWP::UserAgent; use Sys::Hostname qw(hostname); use Tie::File; -my $VERSION = '0.04'; +my $VERSION = '0.05'; -#----------------------- -# Start of configuration -#----------------------- - -my $config_file = 'dynuser.conf'; -my $hosts_file = 'hosts'; -my $session_file = 'session.dat'; -my $server_url = 'http://refcnt.org/~sts/cgi-bin/ketterle/server.cgi'; - -#--------------------- -# End of configuration -#--------------------- +my $conf_file = catfile($Bin, 'client.conf'); sub usage { @@ -61,9 +51,29 @@ my %opts; GetOptions(\%opts, qw(d|debug h|help i|init)) or usage(); usage() if $opts{h}; -$config_file = rel2abs($config_file); -$hosts_file = rel2abs($hosts_file); -$session_file = rel2abs($session_file); +my $config = Config::Tiny->new; + $config = Config::Tiny->read($conf_file); + +my $get_config_opts = sub +{ + my ($section, $options) = @_; + + die "$0: Section $section missing in $conf_file\n" unless exists $config->{$section}; + + my %options; + @options{@$options} = @{$config->{$section}}{@$options}; + + foreach my $option (@$options) { + die "$0: Option $option not set in $conf_file\n" unless defined $options{$option} && length $options{$option}; + } + + return @options{@$options}; +}; + +my ($hosts_file, $session_file) = map rel2abs($_, $Bin), $get_config_opts->('path', [ qw(hosts_file session_file) ]); + +my ($server_url) = $get_config_opts->('url', [ qw(server_url) ]); +my ($netz, $name) = $get_config_opts->('data', [ qw(netz name) ]); my $save_session = sub { @@ -86,13 +96,6 @@ my $get_session = sub my $session = $opts{i} ? substr(md5_hex(md5_hex(time() . {} . rand() . $$)), 0, 32) : $get_session->(); -my $config = Config::Tiny->new; - $config = Config::Tiny->read($config_file); - -my ($netz, $name) = @{$config->{data}}{qw(netz name)}; - -die "$0: Network and/or name not set in $config_file\n" unless defined $netz && defined $name; - my %params = ( netz => $netz, pc => hostname(), diff --git a/dynuser.conf b/dynuser.conf deleted file mode 100644 index 87d130a..0000000 --- a/dynuser.conf +++ /dev/null @@ -1,3 +0,0 @@ -[data] -netz = netz1 -name = name1 diff --git a/server.cgi b/server.cgi index f52f3d9..bdd9ac7 100755 --- a/server.cgi +++ b/server.cgi @@ -19,23 +19,18 @@ use strict; use warnings; +use lib qw(lib); use CGI (); +use Config::Tiny (); use Fcntl ':flock'; +use File::Spec::Functions qw(catfile rel2abs); +use FindBin qw($Bin); use JSON qw(decode_json encode_json); -my $VERSION = '0.04'; +my $VERSION = '0.05'; -#----------------------- -# Start of configuration -#----------------------- - -my $json_file = 'data.json'; -my $session_file = 'session.dat'; - -#--------------------- -# End of configuration -#--------------------- +my $conf_file = catfile($Bin, 'server.conf'); my $query = CGI->new; @@ -56,6 +51,24 @@ if ($params{debug}) { }; } +my $config = Config::Tiny->new; + $config = Config::Tiny->read($conf_file); + +my $section = 'path'; + +die "Section $section missing in $conf_file\n" unless exists $config->{$section}; + +my @options = qw(json_file session_file); + +my %options; +@options{@options} = @{$config->{$section}}{@options}; + +foreach my $option (@options) { + die "Option $option not set in $conf_file\n" unless defined $options{$option} && length $options{$option}; +} + +my ($json_file, $session_file) = map rel2abs($options{$_}, $Bin), @options; + if ($params{init}) { die "Delete server-side $session_file first\n" if -e $session_file; diff --git a/server.conf b/server.conf new file mode 100644 index 0000000..d4f0c20 --- /dev/null +++ b/server.conf @@ -0,0 +1,3 @@ +[path] +json_file = data.json +session_file = session.dat -- 2.39.2