4

The Synopsis for the Plack::Builder and also 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 tried to run it with plackup got:

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.

What is wrong?

Community
  • 1
  • 1
cajwine
  • 3,100
  • 1
  • 20
  • 41

1 Answers1

5

I don't see this mentioned explicitly in the documentation, but you have to include a path component in addition to the hostname, e.g. http://myhost.com/foo. Change

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

to

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

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

The relevant code is in Plack::App::URLMap (mount simply calls Plack::App::URLMap's map method):

if ($location =~ m!^https?://(.*?)(/.*)!) {
    $host     = $1;
    $location = $2;
}
ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
  • OMG. This is really a stupid bug. ;( Would be nice to have this explicitly documented. Thank you! The error message is confusing - it talks about the _start_. ;) – cajwine Oct 01 '15 at 18:47
  • 1
    I don't know if it's a bug, per se, although the documentation could be clearer. You're supposed to provide both a hostname and a location. `http://myhost.com/` corresponds to `/` on `myhost.com`; `http://myhost.com` doesn't specify a location, just a hostname. – ThisSuitIsBlackNot Oct 01 '15 at 19:02
  • 1
    mean bug in _my_ code :) - is has logic to have a `/` at the end. Just the doc should be cleaner. Thanx. :) – cajwine Oct 01 '15 at 19:29
  • 1
    Well don't hesitate to open a pull request or issue to make the error less confusing :) https://github.com/plack/Plack – miyagawa Oct 02 '15 at 22:30