typester (owner)

Revisions

gist: 222021 Download_button fork
public
Public Clone URL: git://gist.github.com/222021.git
Embed All Files: show embed
path-attrrouter.pl #
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
use AnyEvent::HTTPD;
use Path::AttrRouter;
 
{
    package Controller;
    use base 'Path::AttrRouter::Controller';
 
    sub not_found :Path :Args {
        my ($self, $req) = @_;
        '404!';
    }
 
    sub index :Path {
        'Hello';
    }
 
    sub hello :Local :Args(1) {
        my ($self, $req, $target) = @_;
 
        "Hello $target!";
    }
}
 
my $httpd = AnyEvent::HTTPD->new(port => 9090);
my $router = Path::AttrRouter->new( search_path => 'Controller' );
 
$router->print_table if $ENV{DEBUG};
 
$httpd->reg_cb (
   '' => sub {
       my ($httpd, $req) = @_;
 
       my $m = $router->match($req->url);
       if ($m) {
           $req->respond({ content => ['text/html', $m->dispatch($req)] });
       }
       else {
           $req->respond(
               [ 404, 'not found', { 'Content-Type' => 'text/plain' },
                   'not found'
               ]
           );
       }
   },
);
 
$httpd->run; # making a AnyEvent condition variable would also work