Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Path::Dispatcher

  • Loading branch information...
commit 286ad380b3b630f5b918efd855d5661e87512200 1 parent dac803f
@sartak sartak authored
Showing with 50 additions and 0 deletions.
  1. +50 −0 path-dispatcher.pl
View
50 path-dispatcher.pl
@@ -0,0 +1,50 @@
+use Plack::Request;
+use Plack::Response;
+
+{
+ 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
+ };
+
+ 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
+ };
+ };
+
+ 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
+ };
+ };
+}
+
+sub {
+ my $req = Plack::Request->new(shift);
+ my $path = Path::Dispatcher::Path->new(
+ path => $req->path_info,
+ metadata => $req->env,
+ );
+
+ my $dispatch = MyApp::Dispatcher->dispatch($path);
+ return Plack::Response->new(404)->finalize if !$dispatch->has_matches;
+
+ my $res = $dispatch->run($req);
+ $res->finalize;
+};
+
Please sign in to comment.
Something went wrong with that request. Please try again.