| Plack documentation | Contained in the Plack distribution. |
Plack::App::Cascade - Cascadable compound application
use Plack::App::Cascade;
use Plack::App::URLMap;
use Plack::App::File;
# Serve static files from multiple search paths
my $cascade = Plack::App::Cascade->new;
$cascade->add( Plack::App::File->new(root => "/www/example.com/foo")->to_app );
$cascade->add( Plack::App::File->new(root => "/www/example.com/bar")->to_app );
my $app = Plack::App::URLMap->new;
$app->map("/static", $cascade);
$app->to_app;
Plack::App::Cascade is a Plack middleware component that compounds several apps and tries them to return the first response that is not 404.
$app = Plack::App::Cascade->new(apps => [ $app1, $app2 ]);
Creates a new Cascade application.
$app->add($app1); $app->add($app2, $app3);
Appends a new application to the list of apps to try. You can pass the
multiple apps to the one add call.
$app->catch([ 403, 404 ]);
Sets which error codes to catch and process onwards. Defaults to 404.
Tatsuhiko Miyagawa
Plack::App::URLMap Rack::Cascade
| Plack documentation | Contained in the Plack distribution. |
package Plack::App::Cascade; use strict; use base qw(Plack::Component); use Plack::Util; use Plack::Util::Accessor qw(apps catch codes); sub add { my $self = shift; $self->apps([]) unless $self->apps; push @{$self->apps}, @_; } sub prepare_app { my $self = shift; my %codes = map { $_ => 1 } @{ $self->catch || [ 404 ] }; $self->codes(\%codes); } sub call { my($self, $env) = @_; return sub { my $respond = shift; my $res = [ 404, [ 'Content-Type' => 'text/html' ], [ '404 Not Found' ] ]; my $done; my $respond_wrapper = sub { my $res = shift; if ($self->codes->{$res->[0]}) { return Plack::Util::inline_object write => sub { }, close => sub { }; } else { $done = 1; return $respond->($res); } }; for my $app (@{$self->apps || []}) { my $res = $app->($env); if (ref $res eq 'CODE') { $res->($respond_wrapper); } else { $respond_wrapper->($res); } return if $done; } $respond->($res); }; } 1; __END__