Plack::Builder的概要和这个答案说:
# in .psgi
use Plack::Builder;
my $app = sub { ... };
builder {
mount "/foo" => builder {
enable "Foo";
$app;
};
mount "/bar" => $app2;
mount "http://example.com/" => builder { $app3 };
};
我试了如下:
use Plack::Builder;
my $app1 = sub { return [200, ['Content-Type' => 'text/plain'], [ "Hello 1"] ]; };
my $app2 = sub { return [200, ['Content-Type' => 'text/plain'], [ "Hello 2"] ]; };
my $app3 = sub { return [200, ['Content-Type' => 'text/plain'], [ "Hello 3"] ]; };
builder {
mount "/a1" => builder { $app1 };
mount "http://myhost.com" => builder{ $app2 };
mount "/" => builder{ $app3 };
}
但是当试图用plackup
运行它时得到:
加载/tmp/app时出错。psgi:路径需要以/at开头/home/cw/.anyenv/envs/plenv/versions/5.20.3/lib/perl5/site_perl/5.20.3/Plack/Builder.pm第108行。
怎么了?
我没有看到文档中明确提到这一点,但是除了主机名之外,您必须包含路径组件,例如http://myhost.com/foo
。把
mount "http://myhost.com" => builder{ $app2 };
mount "http://myhost.com/" => builder{ $app2 };
(即。/
on host myhost.com
)
相关代码在Plack::App::URLMap (mount
只是调用Plack::App::URLMap的map
方法):
if ($location =~ m!^https?://(.*?)(/.*)!) {
$host = $1;
$location = $2;
}