Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement --path for plackup to mount $app under a certain path. Corr…

…ect fix for #337
  • Loading branch information...
commit cbc0ba437306334e8b9998e3da70f4ca88aa5ae4 1 parent 0c38e0d
@miyagawa miyagawa authored
Showing with 51 additions and 0 deletions.
  1. +10 −0 lib/Plack/Runner.pm
  2. +41 −0 t/Plack-Runner/path.t
View
10 lib/Plack/Runner.pm
@@ -55,6 +55,7 @@ sub parse_options {
'R|Reload=s' => sub { $self->{loader} = "Restarter"; $self->loader->watch(split ",", $_[1]) },
'L|loader=s' => \$self->{loader},
"access-log=s" => \$self->{access_log},
+ "path=s" => \$self->{path},
"h|help" => \$self->{help},
"v|version" => \$self->{version},
"default-middleware!" => \$self->{default_middleware},
@@ -248,6 +249,15 @@ sub run {
my $app = $self->locate_app(@args);
+ if ($self->{path}) {
+ require Plack::App::URLMap;
+ $app = build {
+ my $urlmap = Plack::App::URLMap->new;
+ $urlmap->mount($self->{path} => $_[0]);
+ $urlmap->to_app;
+ } $app;
+ }
+
$ENV{PLACK_ENV} ||= $self->{env} || 'development';
if ($ENV{PLACK_ENV} eq 'development') {
$app = $self->prepare_devel($app);
View
41 t/Plack-Runner/path.t
@@ -0,0 +1,41 @@
+use strict;
+use Cwd;
+use File::Spec;
+use File::Temp;
+use LWP::UserAgent;
+use Test::More;
+use Test::TCP qw(empty_port);
+
+plan skip_all => "developer only" unless -e '.git';
+
+sub write_file($$){
+ my ( $path, $content ) = @_;
+ open my $out, '>', $path or die "$path: $!";
+ print $out $content;
+ close $out;
+}
+
+my $tmpdir = File::Temp::tempdir( CLEANUP => 1 );
+my $psgi_file = File::Spec->catfile($tmpdir, 'app.psgi');
+write_file $psgi_file, qq/my \$app = sub {return [200, [], ["hello world"]]}\n/;
+
+my $port = empty_port();
+my $pid = fork;
+if ($pid == 0) {
+ close STDERR;
+ exec($^X, 'bin/plackup', '-p', $port, '--path', '/app/', '-a', $psgi_file) or die $@;
+} else {
+ $SIG{INT} = 'IGNORE';
+ sleep 1;
+ my $ua = LWP::UserAgent->new;
+ my $res = $ua->get("http://localhost:$port/");
+ is $res->code, 404;
+ $res = $ua->get("http://localhost:$port/app/");
+ is $res->code, 200;
+ is $res->content, 'hello world';
+ kill 'INT', $pid;
+ wait;
+}
+
+done_testing;
+
Please sign in to comment.
Something went wrong with that request. Please try again.