-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.psgi
executable file
·103 lines (82 loc) · 2.03 KB
/
router.psgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use strict;
use warnings;
use Plack::Builder;
use Router::Simple;
use Text::Handlebars;
use File::Slurp;
use PayDerbyDues::Auth::Middleware;
use PayDerbyDues::Utilities::DBConnect;
use PayDerbyDues::RequestGlobalData;
use PayDerbyDues::Constants;
use PayDerbyDues::GlobalRouter;
my $router = PayDerbyDues::GlobalRouter::_GetGlobalRouter();
my $dbh;
# add wrapper to engage the resource-specific logic
my $app = sub {
my $env = shift;
if ($env->{match}) {
my $rv = eval {
$env->{match}{func}->($env->{match}, $env);
};
if ($@) {
$rv = [
$PayDerbyDues::Constants::HTTP_INTERNAL_ERROR_STATUS,
$PayDerbyDues::Constants::PLAIN_CONTENT_TYPE_HEADER,
["ERROR!!! $@"],
];
}
return $rv;
}
};
# add global request data initialization wrapper
my $app1 = sub {
my $env = shift;
PayDerbyDues::RequestGlobalData::InitializeRequestGlobalData({
DBH => $dbh,
MEMBERID => $env->{memberid},
});
return $app->($env);
};
# add authentication middleware wrapper
my $app2 = sub {
my $env = shift;
my %config = (
unauthredirect => '/',
timeout_sec => 30 * 60,
);
$env->{memberid} = eval { PayDerbyDues::Auth::Middleware::check_auth($env, $dbh, %config) };
if ($env->{match}{requires_auth}) {
unless (defined($env->{memberid})) {
my $res = Plack::Response->new;
$res->redirect($config{unauthredirect});
warn "Unauthenticated user detected";
return $res->finalize;
}
}
return $app1->($env);
};
# add $dbh-creation middleware wrapper
my $app3 = sub {
my $env = shift;
$dbh = PayDerbyDues::Utilities::DBConnect::GetDBH();
return $app2->($env);
};
# add 404 middleware wrapper
my $app4 = sub {
my $env = shift;
my $match = $router->match($env);
unless ($match) {
return [
$PayDerbyDues::Constants::HTTP_NOT_FOUND_STATUS,
$PayDerbyDues::Constants::HTML_CONTENT_TYPE_HEADER,
[
Text::Handlebars->new()->render_string(
File::Slurp::read_file('/home/ec2-user/payderbydues/www/handlebarstemplates/404.hbs'),
{},
),
]
];
}
$env->{match} = $match;
return $app3->($env);
};