Install "hosts" in Plack :: Builder

The summary for Plack :: Builder as well as this answer says:

# in .psgi
use Plack::Builder;

my $app = sub { ... };

builder {
    mount "/foo" => builder {
        enable "Foo";
        $app;
    };

    mount "/bar" => $app2;
    mount "http://example.com/" => builder { $app3 };
};

I tried the following:

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 };
}

But when I tried to start it with the help I plackupgot:

Error loading /tmp/app.psgi: Paths should start with / at /home/cw/.anyenv/envs/plenv/versions/5.20.3/lib/perl5/site_perl/5.20.3/Plack/Builder.pm line 108.

What's wrong?

+4
source share
1 answer

I do not see this explicitly in the documentation, but you must include the path component in addition to the host name, for example. http://myhost.com/foo. Change

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

to

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

(i.e. /on the host myhost.com)

Plack:: App:: URLMap (mount Plack:: App:: URLMap map ):

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

Source: https://habr.com/ru/post/1609840/


All Articles