Permalink
Browse files

share the controller backend in Path::Dispatcher, HTTPx::Dispatcher a…

…nd HTTP::Router
  • Loading branch information...
1 parent 286ad38 commit f45bc6f0f07ca02ab7896fa97254ae4f14076da0 @miyagawa committed Mar 16, 2010
Showing with 60 additions and 70 deletions.
  1. +11 −22 http-router-declare.pl
  2. +5 −33 httpx-dispatcher.pl
  3. +24 −0 lib/MyApp/Blog.pm
  4. +13 −0 lib/MyApp/Hello.pm
  5. +6 −15 path-dispatcher.pl
  6. +1 −0 tester.pl
View
33 http-router-declare.pl
@@ -2,29 +2,16 @@
use strict;
use Plack::Request;
use HTTP::Router::Declare;
+use MyApp::Blog;
+use MyApp::Hello;
my $router = router {
- 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;
- } };
+ 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 => 'comment' };
};
sub {
@@ -33,6 +20,8 @@
or return $req->new_response(404)->finalize;
my $p = $match->params;
- my $res = $p->{action}->($req, $p);
+ my $controller = "MyApp::" . $p->{controller};
+ my $action = $controller->can(lc($req->method) . "_" . $p->{action});
+ my $res = $controller->$action($req, $p);
$res->finalize;
};
View
38 httpx-dispatcher.pl
@@ -2,52 +2,24 @@
use strict;
use Plack::Request;
-use lib "lib";
+use MyApp::Hello;
+use MyApp::Blog;
{
package MyApp::Disptacher;
use HTTPx::Dispatcher;
connect '' => { controller => 'Hello', action => 'index'};
connect 'blog/:year/:month' => { controller => 'Blog', action => 'monthly' };
- connect 'comment' => { controller => 'Blog', action => 'new_comment' };
-}
-
-{
- package MyApp::C::Hello;
- sub index {
- my ($class, $req, $p) = @_;
- my $res = $req->new_response(200);
- $res->content_type("text/plain");
- $res->content("Hello World");
- $res;
- }
-}
-{
- package MyApp::C::Blog;
- sub monthly {
- my($class, $req, $p) = @_;
- return $req->new_response(404) if $req->method eq 'POST';
- 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;
- }
+ connect 'comment' => { controller => 'Blog', action => 'comment' };
}
sub {
my $req = Plack::Request->new(shift);
my $match = MyApp::Disptacher->match($req)
or return $req->new_response(404)->finalize;
- my $controller = "MyApp::C::$match->{controller}";
- my $action = $match->{action};
+ my $controller = "MyApp::$match->{controller}";
+ my $action = $controller->can(lc($req->method) . "_" . $match->{action});
my $res = $controller->$action($req, $match->{args});
$res->finalize;
};
View
24 lib/MyApp/Blog.pm
@@ -0,0 +1,24 @@
+package MyApp::Blog;
+
+sub get_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 post_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
@@ -0,0 +1,13 @@
+package MyApp::Hello;
+
+sub get_index {
+ my($class, $req) = @_;
+
+ my $res = $req->new_response(200);
+ $res->content_type("text/plain");
+ $res->content("Hello World");
+
+ return $res;
+}
+
+1;
View
21 path-dispatcher.pl
@@ -1,35 +1,26 @@
use Plack::Request;
use Plack::Response;
+use MyApp::Blog;
+use MyApp::Hello;
+
{
package MyApp::Dispatcher;
use Path::Dispatcher::Declarative -base, -default => {
token_delimiter => '/',
};
- on [] => sub {
- my $res = Plack::Response->new(200);
- $res->content_type('text/plain');
- $res->content("Hello World");
- $res
- };
+ on [] => sub { MyApp::Hello->get_index(@_) };
under { REQUEST_METHOD => 'GET' } => sub {
on ['blog', qr/^\d+$/, qr/^\d+$/] => sub {
- my $res = Plack::Response->new(200);
- $res->content_type('text/html');
- $res->content("Blog posts from $2/$3");
- $res
+ MyApp::Blog->get_monthly(shift, { year => $2, month => $3 });
};
};
under { REQUEST_METHOD => 'POST' } => sub {
on 'comment' => sub {
- my $req = shift;
- my $res = Plack::Response->new(200);
- $res->content_type('text/plain');
- $res->content("Comment posted with body=" . $req->param('body'));
- $res
+ MyApp::Blog->post_comment(@_);
};
};
}
View
1 tester.pl
@@ -1,3 +1,4 @@
+use lib "lib";
use Plack::Test;
use Plack::Util;
use HTTP::Request::Common;

0 comments on commit f45bc6f

Please sign in to comment.