Skip to content

Commit 1fdf5d7

Browse files
committed
Added config file
1 parent f6d4bcf commit 1fdf5d7

File tree

6 files changed

+75
-15
lines changed

6 files changed

+75
-15
lines changed

ADBOS/Auth.pm

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use Apache::Session::File;
77
use CGI::Cookie;
88
use ADBOS::Display;
99
use ADBOS::DB;
10+
use ADBOS::Config;
1011
use Crypt::SaltedHash;
1112
use String::Random;
1213
use DateTime;
@@ -48,7 +49,8 @@ sub login()
4849
my $message;
4950
if($q->body('login')) # Only POST requests
5051
{
51-
my $db = ADBOS::DB->new;
52+
my $config = simple_config;
53+
my $db = ADBOS::DB->new($config);
5254
my $user = $db->userGet({ username => $q->param('username') });
5355
if ($user && Crypt::SaltedHash->validate($user->password, $q->param('password')))
5456
{

ADBOS/Config.pm

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use warnings;
2+
use strict;
3+
4+
package ADBOS::Config;
5+
use base 'Exporter';
6+
7+
our @EXPORT = qw(simple_config);
8+
9+
sub simple_config
10+
{ my $fn = shift || '/etc/adbos.conf';
11+
12+
open CONFIG, '<:encoding(utf8)', $fn
13+
or die "cannot read config from $fn";
14+
15+
my %config;
16+
while(<CONFIG>)
17+
{ next if m/^#|^\s*$/;
18+
19+
# as long as the lines contain a trailing \, concat more lines
20+
$_ .= <CONFIG> while s/\\$//;
21+
22+
# remove last new-line
23+
chomp;
24+
25+
# key-value separated by " = " or just blanks
26+
my ($key, $value) = split /(?:\s+\=\s+|\s+)/, $_, 2;
27+
28+
# expands ${SOMETHING} constructs from config or %ENV
29+
$value =~ s/\$\{(\w+)\}/expand_var(\%config,$1)/ge;
30+
31+
# change text "undef" into value undef
32+
undef $value if $value eq 'undef';
33+
34+
if(!$config{$key})
35+
{ # key encountered for the first time
36+
$config{$key} = $value;
37+
}
38+
elsif(ref $config{$key} eq 'ARRAY')
39+
{ # key already found twice or more often
40+
push @{$config{$key}}, $value;
41+
}
42+
else
43+
{ # key found for the second time: upscale to array
44+
$config{$key} = [ $config{$key}, $value ];
45+
}
46+
}
47+
close CONFIG
48+
or die "read errors for $fn";
49+
50+
\%config;
51+
}
52+
53+
sub expand_var($$)
54+
{ my ($config, $var) = @_;
55+
my $val = $config->{$var} // $ENV{$var}
56+
or die "expand variable $var not (yet) known";
57+
$val;
58+
}

ADBOS/DB.pm

+7-11
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,21 @@ OPTIONS:
2424
=cut
2525

2626
sub new($%)
27-
{ my ($class, $global_config, %args) = @_;
28-
# $config = $global_config || {};
27+
{ my ($class, $config) = @_;
2928
my $self = bless {}, $class;
30-
# my ($dbname, $dbuser, $dbpass) = @{$config}{qw/DBNAME DBUSER DBPASS/};
31-
# $self->{SB_name} = $args{name} || $config->{DBNAME};
32-
# $self->{SB_user} = $args{user} || $config->{DBUSER};
33-
# $self->{SB_pass} = $args{password} || $config->{DBPASS};
34-
$self->{SB_name} = 'opdef';
35-
$self->{SB_user} = 'opdef';
36-
$self->{SB_pass} = 'cunteryun';
29+
$self->{dbhost} = $config->{dbhost} or die 'Need dbhost';
30+
$self->{dbname} = $config->{dbname} or die 'Need dbname';
31+
$self->{dbuser} = $config->{dbuser} or die 'Need dbuser';
32+
$self->{dbpass} = $config->{dbpass} or die 'Need dbpass';
3733
$self;
3834
}
3935

4036
sub connect()
4137
{ my $self = shift;
42-
my $dbname = $self->{SB_name};
38+
my $dbname = $self->{dbname};
4339

4440
$self->{sch} = ADBOS::Schema->connect(
45-
"dbi:mysql:database=$dbname;host=adbosdb", $self->{SB_user}, $self->{SB_pass}
41+
"dbi:mysql:database=$dbname;host=".$self->{dbhost}, $self->{dbuser}, $self->{dbpass}
4642
, {RaiseError => 1, AutoCommit => 1, mysql_enable_utf8 => 1}
4743
) or error __x"unable to connect to database {name}: {err}"
4844
, name => $dbname, err => $DBI::errstr;

ADBOS/Display.pm

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use Template;
77

88
use ADBOS::DB;
99
use ADBOS::Parse;
10+
use ADBOS::Config;
1011

1112
=pod
1213
@@ -23,7 +24,8 @@ OPTIONS:
2324
2425
=cut
2526

26-
my $db = ADBOS::DB->new;
27+
my $config = simple_config;
28+
my $db = ADBOS::DB->new($config);
2729

2830
sub new($$)
2931
{ my ($class, $q) = @_;

process.pl

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55

66
use ADBOS::DB;
77
use ADBOS::Parse;
8+
use ADBOS::Config;
89
use File::Slurp;
910

1011
sub process($;$);
1112

13+
my $config = simple_config;
14+
1215
my $parser = ADBOS::Parse->new();
13-
my $db = ADBOS::DB->new();
16+
my $db = ADBOS::DB->new($config);
1417

1518
# See if text is being piped in. If so, read it
1619
if (not -t STDIN and my $message = do { local $/; <STDIN> })

testparse.pl

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Data::Dumper;
1010

1111
my $parser = ADBOS::Parse->new();
12-
my $db = ADBOS::DB->new();
1312

1413
my $message = do { local $/; <STDIN> };
1514

0 commit comments

Comments
 (0)