public
Description: A Perl 6 web framework
Homepage:
Clone URL: git://github.com/masak/web.git
web / lib / LolDispatch.pm
100644 45 lines (36 sloc) 0.98 kb
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
role http-handler {
}
 
# Needs a better name
module LolDispatch {
    my @routes;
    multi trait_auxiliary:<is>(http-handler $trait, $block, $arg) is export {
        @routes.push({:route($arg[0]), :block($block)});
    }
 
    sub dispatch($r) is export {
        for @routes -> $item{
            if $r.url.path ~~ $item<route> {
                my $ret = $item<block>($r,$/);
                return $ret;
            }
        }
        warn "Could not dispatch {$r.url.path}";
    }
}
 
=begin usage
use LolDispatch;
use HTTP::Daemon;
 
sub foo($request, $match) is http-handler(/wtf/) {
    say 'dispatched to foo';
    say $match.perl;
}
 
sub item($request, $match) is http-handler(/^\/item\/(\d+)/) {
    say 'dispatched to item';
    say $match.perl;
}
 
my $request = HTTP::Request.new(
    req_url => HTTP::url.new(path => '/item/12345'),
    headers => HTTP::Headers.new( header_values => { 'Host' => 'localhost' }),
    req_method => 'GET',
);
 
dispatch($request);
=end