Created
          October 30, 2009 01:54 
        
      - 
      
- 
        Save typester/222021 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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