Skip to content

Instantly share code, notes, and snippets.

@typester
Created October 30, 2009 01:54
Show Gist options
  • Save typester/222021 to your computer and use it in GitHub Desktop.
Save typester/222021 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment