Plack::App::Path::Router::PSGI - A Plack component for dispatching with Path::Router to Pure PSGI targets
use Plack::App::Path::Router::PSGI; use Path::Router; my $router = Path::Router->new; $router->add_route('/' => target => sub { my $env = shift; [ 200, [ 'Content-Type' => 'text/html' ], [ '<html><body>Home</body></html>' ] ] } ); $router->add_route('/:action/?:id' => validations => { id => 'Int' }, target => sub { my $env = shift; # matches are passed through the $env my ($action, $id) = @{ $env->{'plack.router.match.args'} }; [ 200, [ 'Content-Type' => 'text/html' ], [ '<html><body>', $action, $id, '</body></html>' ] ] } ); $router->add_route('admin/:action/?:id' => validations => { id => 'Int' }, # targets are just PSGI apps, so you can # wrap with middleware as needed ... target => Plack::Middleware::Auth::Basic->wrap( sub { my $env = shift; # matches are passed through the $env my ($action, $id) = @{ $env->{'plack.router.match.args'} }; [ 200, [ 'Content-Type' => 'text/html' ], [ '<html><body>', $action, $id, '</body></html>' ] ] }, authenticator => sub { my ($username, $password) = @_; return $username eq 'admin' && $password eq 's3cr3t'; } ) ); # now create the Plack app my $app = Plack::App::Path::Router::PSGI->new( router => $router );
This is a Plack::Component subclass which creates an endpoint to dispatch using Path::Router.
This module is similar to Plack::App::Path::Router except that it expects all the route targets to be pure PSGI apps, nothing more, nothing less. Which means that they will accept a single $env argument and return a valid PSGI formatted response.
$env
This will place, into the $env the router instance into 'plack.router', any valid match in 'plack.router.match' and the collected URL match args in 'plack.router.match.args'.
This thing is dead simple, if my docs don't make sense, then just read the source (all ~45 lines of it).
This is a required attribute and must be an instance of Path::Router.
All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.
Stevan Little <stevan.little@iinteractive.com>
Copyright 2009-2011 Infinity Interactive, Inc.
http://www.iinteractive.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Plack::App::Path::Router, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Plack::App::Path::Router
CPAN shell
perl -MCPAN -e shell install Plack::App::Path::Router
For more information on module installation, please visit the detailed CPAN module installation guide.