Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add “object” type of argument for Plack::Runner #416

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/Plack/Runner.pm
Expand Up @@ -160,6 +160,10 @@ sub locate_app {
return sub { $psgi }; return sub { $psgi };
} }


if (Scalar::Util::blessed($psgi) and $psgi->can('to_app')){
return sub { $psgi->to_app };
}

if ($self->{eval}) { if ($self->{eval}) {
$self->loader->watch("lib"); $self->loader->watch("lib");
return build { return build {
Expand Down Expand Up @@ -295,6 +299,20 @@ Plack::Runner - plackup core
$runner->parse_options(@ARGV); $runner->parse_options(@ARGV);
$runner->run($app); $runner->run($app);


# use app builder instead of app
use Plack::Runner;
my $app_builder => sub {
# do some thing here will run every time server loaded

return sub {
# the real app code here
}
};

my $runner = Plack::Runner->new;
$runner->parse_options(@ARGV);
$runner->run(\$app_builder); # use code ref ref

=head1 DESCRIPTION =head1 DESCRIPTION


Plack::Runner is the core of L<plackup> runner script. You can create Plack::Runner is the core of L<plackup> runner script. You can create
Expand Down
83 changes: 83 additions & 0 deletions t/Plack-Runner/app_builder.t
@@ -0,0 +1,83 @@

use Test::More;
use Plack::Runner;
use File::Path (qw/make_path remove_tree/);
use Test::Requires (qw/LWP::UserAgent Test::MockObject/);

make_path "tmp/m";

my $app = Test::MockObject->new();
$app->mock(
to_app => sub {
eval {
open my $fh, ">>", "tmp/counter";
print $fh "1";
close $fh;
};
return sub {
return [ 200, [], ["Hello"] ];
}
}
);

my $runner = Plack::Runner->new;

sub counter_ok {
my ( $len, $msg ) = @_;
my $content = "";
eval {
open my $fh, "<", "tmp/counter";
$content = <$fh>;

};
is $content , "1" x $len, $msg;
}

$runner->parse_options( -R => 'tmp/m', '-r', -p => 5000 );

my $pid = fork;

if ($pid) {
my $timeout = 5;
until ( -f "tmp/counter" ) {
sleep 1;
last if $timeout-- == 0;
}
if ( $timeout < 0 ) {
is 1 < 0, "plack up server failed";
kill 'TERM' => $pid;
}
else {
my $ua = LWP::UserAgent->new;
my $res = $ua->get('http://127.0.0.1:5000/');
ok $res->is_success;
is $res->code, 200;
is $res->content, 'Hello';

counter_ok 1, "app builder not run";

make_path "tmp/m/a";
sleep 5;

counter_ok 2, "app builder run";
$res = $ua->get('http://127.0.0.1:5000/');
ok $res->is_success;
is $res->code, 200;
is $res->content, 'Hello';

remove_tree "tmp/m/a";
sleep 5;

counter_ok 3, "app builder run again";

kill 'TERM' => $pid;
}
}
else {
$runner->run($app);
exit 0;
}

remove_tree "tmp";
done_testing;