Mount"主持人"在Plack :: Builder中

时间:2015-10-01 18:13:55

标签: perl plack psgi

Plack::Builderthis 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, ['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行。

有什么问题?

1 个答案:

答案 0 :(得分:5)

我没有在文档中明确提到这一点,但除主机名外还必须包含一个路径组件,例如: http://myhost.com/foo。变化

mount "http://myhost.com" => builder{ $app2 };

mount "http://myhost.com/" => builder{ $app2 };

(即主持人/上的myhost.com

相关代码位于Plack::App::URLMapmount只调用Plack :: App :: URLMap的map方法):

if ($location =~ m!^https?://(.*?)(/.*)!) {
    $host     = $1;
    $location = $2;
}