|
NAMEPlack::App::URLMap - Map multiple apps in different paths SYNOPSIS use Plack::App::URLMap;
my $app1 = sub { ... };
my $app2 = sub { ... };
my $app3 = sub { ... };
my $urlmap = Plack::App::URLMap->new;
$urlmap->map("/" => $app1);
$urlmap->map("/foo" => $app2);
$urlmap->map("http://bar.example.com/" => $app3);
my $app = $urlmap->to_app;
DESCRIPTIONPlack::App::URLMap is a PSGI application that can dispatch multiple applications based on URL path and host names (a.k.a "virtual hosting") and takes care of rewriting "SCRIPT_NAME" and "PATH_INFO" (See "HOW THIS WORKS" for details). This module is inspired by Ruby's Rack::URLMap. METHODS
PERFORMANCEIf you "map" (or "mount" with Plack::Builder) N applications, Plack::App::URLMap will need to at most iterate through N paths to match incoming requests. It is a good idea to use "map" only for a known, limited amount of applications, since mounting hundreds of applications could affect runtime request performance. DEBUGGINGYou can set the environment variable "PLACK_URLMAP_DEBUG" to see how this application matches with the incoming request host names and paths. HOW THIS WORKSThis application works by fixing "SCRIPT_NAME" and "PATH_INFO" before dispatching the incoming request to the relocated applications. Say you have a Wiki application that takes "/index" and "/page/*" and makes a PSGI application $wiki_app out of it, using one of supported web frameworks, you can put the whole application under "/wiki" by: # MyWikiApp looks at PATH_INFO and handles /index and /page/*
my $wiki_app = sub { MyWikiApp->run(@_) };
use Plack::App::URLMap;
my $app = Plack::App::URLMap->new;
$app->mount("/wiki" => $wiki_app);
When a request comes in with "PATH_INFO" set to "/wiki/page/foo", the URLMap application $app strips the "/wiki" part from "PATH_INFO" and appends that to "SCRIPT_NAME". That way, if the $app is mounted under the root (i.e. "SCRIPT_NAME" is "") with standalone web servers like Starman, "SCRIPT_NAME" is now locally set to "/wiki" and "PATH_INFO" is changed to "/page/foo" when $wiki_app gets called. AUTHORTatsuhiko Miyagawa SEE ALSOPlack::Builder
|