summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2017-12-10 08:05:54 +0100
committerYorhel <git@yorhel.nl>2017-12-10 08:05:54 +0100
commitfe37312dcb60f97be0b17aaa6cdb0db115be7d6a (patch)
treee9ad751ef1bf95e806103a1df95e76a481eead01 /lib
parentf1d83e26ba17cfe60218221863c92de71d9c7eee (diff)
Store route handlers in $TUWF::OBJ + add error checking in TUWF::any
This moves *all* of an applications config & state into the TUWF object, thus making it possible (but still quicky) to host multiple sites in one process by assigning different objects to $TUWF::OBJ. And while I'm normally not a huge fan of run-time checking of function arguments, the TUWF::any() function typically is only ever called at program initialization. This may catch some bugs that might otherwise be a pain to debug.
Diffstat (limited to 'lib')
-rw-r--r--lib/TUWF.pm17
1 files changed, 10 insertions, 7 deletions
diff --git a/lib/TUWF.pm b/lib/TUWF.pm
index ed2024f..a3b2176 100644
--- a/lib/TUWF.pm
+++ b/lib/TUWF.pm
@@ -18,6 +18,7 @@ our $VERSION = '1.1';
# a mod_perl environment, which we don't support.
our $OBJ = bless {
_TUWF => {
+ route_handlers => [],
# defaults
mail_from => '<noreply-yawf@blicky.net>',
mail_sendmail => '/usr/sbin/sendmail',
@@ -70,8 +71,6 @@ our $OBJ = bless {
}
}, 'TUWF::Object';
-my @handlers;
-
sub import {
my $self = shift;
@@ -143,7 +142,7 @@ sub run {
sub register {
my $a = \@_;
for my $i (0..$#$a/2) {
- push @handlers,
+ push @{$OBJ->{_TUWF}{route_handlers}},
qr{^(?:GET|POST|HEAD) /$a->[$i*2]$},
sub { $a->[$i*2+1]->($OBJ, @{$OBJ->{_TUWF}{captures_pos}}) }
}
@@ -153,9 +152,12 @@ sub register {
# Register router handlers
sub any {
my($methods, $path, $sub) = @_;
+ croak 'Methods argument in route registration must be an array' if ref $methods ne 'ARRAY';
+ croak 'Path argument in route registration must be a string or regex' if ref $path && ref $path ne 'Regexp';
+ croak 'Subroutine argument in route registration must be a code reference' if ref $sub ne 'CODE';
my $methods_re = '(?:' . join('|', map uc, @$methods). ')';
my $path_re = ref $path eq 'Regexp' ? $path : quotemeta $path;
- push @handlers, qr{^$methods_re $path_re$}, $sub;
+ push @{$OBJ->{_TUWF}{route_handlers}}, qr{^$methods_re $path_re$}, $sub;
}
sub get ($&) { any ['get','head'], @_ }
@@ -314,13 +316,14 @@ sub _handle_request {
my $han = $self->{_TUWF}{error_404_handler};
$self->{_TUWF}{captures_pos} = [];
$self->{_TUWF}{captures_named} = {};
- for (@handlers ? 0..$#handlers/2 : ()) {
- if($loc =~ $handlers[$_*2]) {
+ my $handlers = $self->{_TUWF}{route_handlers};
+ for (@$handlers ? 0..$#$handlers/2 : ()) {
+ if($loc =~ $handlers->[$_*2]) {
$self->{_TUWF}{captures_pos} = [
map defined $-[$_] ? substr $loc, $-[$_], $+[$_]-$-[$_] : undef, 1..$#-
];
$self->{_TUWF}{captures_named} = { %+ };
- $han = $handlers[$_*2+1];
+ $han = $handlers->[$_*2+1];
last;
}
}