Plack::Builder和
this answer的概要说:
# 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,[ "Hello 2"] ]; }; my $app3 = sub { return [200,[ "Hello 3"] ]; }; builder { mount "/a1" => builder { $app1 }; mount "http://myhost.com" => builder{ $app2 }; mount "/" => builder{ $app3 }; }
但是当试图用plackup运行时得到了:
Error while loading /tmp/app.psgi: Paths need to start with / at
/home/cw/.anyenv/envs/plenv/versions/5.20.3/lib/perl5/site_perl/5.20.3/Plack/Builder.pm
line 108.
怎么了?
解决方法
我没有在文档中明确提到这一点,但除主机名外还必须包含路径组件,例如: http://myhost.com/foo.更改
mount "http://myhost.com" => builder{ $app2 };
至
mount "http://myhost.com/" => builder{ $app2 };
(即/在主机myhost.com上)
相关代码在Plack::App::URLMap(mount只调用Plack :: App :: URLMap的map方法):
if ($location =~ m!^https?://(.*?)(/.*)!) { $host = $1; $location = $2; }