Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

inline HTTP::Router::Declare callbacks

  • Loading branch information...
commit 6b870f22211d5fcf31a3d9edf7d054bca7ed8925 1 parent 4fc84bf
@miyagawa authored
Showing with 22 additions and 46 deletions.
  1. +22 −9 http-router-declare.pl
  2. +0 −24 lib/MyApp/Blog.pm
  3. +0 −13 lib/MyApp/Hello.pm
View
31 http-router-declare.pl
@@ -8,12 +8,27 @@
use MyApp::Hello;
my $router = router {
- match '/', { method => 'GET' },
- to { controller => 'Hello', action => 'index' };
- match '/blog/{year}/{month}', { method => 'GET' },
- to { controller => 'Blog', action => 'monthly' };
- match '/comment', { method => 'POST' },
- to { controller => 'Blog', action => 'new_comment' };
+ match '/', { method => 'GET' }, to { action => sub {
+ my($req, $p) = @_;
+ my $res = $req->new_response(200);
+ $res->content_type("text/plain");
+ $res->content("Hello World");
+ $res;
+ } };
+ match '/blog/{year}/{month}', { method => 'GET' }, to { action => sub {
+ my($req, $p) = @_;
+ my $res = $req->new_response(200);
+ $res->content_type('text/html');
+ $res->content("Blog posts from $p->{year}/$p->{month}");
+ $res;
+ } };
+ match '/comment', { method => 'POST' }, to { action => sub {
+ my($req, $p) = @_;
+ my $res = $req->new_response(200);
+ $res->content_type('text/plain');
+ $res->content("Comment posted with body=" . $req->parameters->{body});
+ $res;
+ } };
};
sub {
@@ -22,8 +37,6 @@
or return $req->new_response(404)->finalize;
my $p = $match->params;
- my $controller = "MyApp::$p->{controller}";
- my $action = $p->{action};
- my $res = $controller->$action($req, $p);
+ my $res = $p->{action}->($req, $p);
$res->finalize;
};
View
24 lib/MyApp/Blog.pm
@@ -1,24 +0,0 @@
-package MyApp::Blog;
-
-sub monthly {
- my($class, $req, $p) = @_;
-
- my $res = $req->new_response(200);
- $res->content_type('text/html');
- $res->content("Blog posts from $p->{year}/$p->{month}");
-
- $res;
-}
-
-sub new_comment {
- my($class, $req, $p) = @_;
-
- my $res = $req->new_response(200);
- $res->content_type('text/plain');
- $res->content("Comment posted with body=" . $req->parameters->{body});
-
- $res;
-}
-
-1;
-
View
13 lib/MyApp/Hello.pm
@@ -1,13 +0,0 @@
-package MyApp::Hello;
-
-sub index {
- my($class, $req) = @_;
-
- my $res = $req->new_response(200);
- $res->content_type("text/plain");
- $res->content("Hello World");
-
- return $res;
-}
-
-1;
Please sign in to comment.
Something went wrong with that request. Please try again.