forked from plack/Plack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.pm
186 lines (146 loc) · 4.83 KB
/
Runner.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package Plack::Runner;
use strict;
use warnings;
use File::Basename;
use Getopt::Long;
use Plack::Loader;
use Plack::Util;
use Try::Tiny;
sub new {
my $class = shift;
bless {}, $class;
}
# delay the build process for reloader
sub build(&;$) {
my $block = shift;
my $app = shift || sub { };
return sub { $block->($app->()) };
}
sub run {
my $self = shift;
$self = $self->new unless ref $self;
local @ARGV = @_;
my $psgi;
my $eval;
my $host;
my $port = 5000;
my $env = "development";
my $help = 0;
my $backend;
my @reload;
my $reload;
my @includes;
my @modules;
# From 'prove': Allow cuddling the paths with -I and -M
@ARGV = map { /^(-[IM])(.+)/ ? ($1,$2) : $_ } @ARGV;
Getopt::Long::Configure("no_ignore_case", "pass_through");
GetOptions(
"a|app=s" => \$psgi,
"o|host=s" => \$host,
"p|port=i" => \$port,
"s|server=s" => \$backend,
"i|impl=s" => sub { warn "-i is deprecated. Use -s instead\n"; $backend = $_[1] },
"E|env=s" => \$env,
"e=s" => \$eval,
'I=s@' => \@includes,
'M=s@' => \@modules,
'r|reload' => sub { $reload = 1 },
'R|Reload=s' => sub { push @reload, split ",", $_[1] },
"h|help", => \$help,
);
if ($help) {
require Pod::Usage;
Pod::Usage::pod2usage(0);
}
lib->import(@includes) if @includes;
if ($eval) {
push @modules, 'Plack::Builder';
}
for (@modules) {
my($module, @import) = split /[=,]/;
eval "require $module" or die $@;
$module->import(@import);
}
my(@options, @argv);
while (defined($_ = shift @ARGV)) {
if (s/^--?//) {
my @v = split '=', $_, 2;
$v[0] =~ tr/-/_/;
if (@v == 2) {
push @options, @v;
} else {
push @options, @v, shift @ARGV;
}
} else {
push @argv, $_;
}
}
push @options, host => $host, port => $port;
$psgi ||= $argv[0] || "app.psgi";
my $app = $eval ? build { no strict; no warnings; eval $eval or die $@ }
: ref $psgi eq 'CODE' ? sub { $psgi }
: build { Plack::Util::load_psgi $psgi };
if ($env eq 'development') {
require Plack::Middleware::StackTrace;
require Plack::Middleware::AccessLog;
$app = build { Plack::Middleware::StackTrace->wrap($_[0]) } $app;
unless ($ENV{GATEWAY_INTERFACE}) {
$app = build { Plack::Middleware::AccessLog->wrap($_[0], logger => sub { print STDERR @_ }) } $app;
}
push @options, server_ready => sub {
my($args) = @_;
my $name = $args->{server_software} || ref($args); # $args is $server
print STDERR "$name: Accepting connections at http://$args->{host}:$args->{port}/\n";
};
}
my $loader;
if ($reload or @reload) {
if ($reload) {
push @reload, $eval ? "lib" : ( File::Basename::dirname($psgi) . "/lib", $psgi );
}
warn "plackup: Watching ", join(", ", @reload), " for changes\n";
require Plack::Loader::Reloadable;
$loader = Plack::Loader::Reloadable->new(\@reload);
} else {
$loader = 'Plack::Loader';
$app = $app->();
}
my $server = $backend ? $loader->load($backend, @options) : $loader->auto(@options);
$server->run($app);
}
1;
__END__
=head1 NAME
Plack::Runner - plackup core
=head1 SYNOPSIS
# Your bootstrap script
use Plack::Runner;
my $app = sub { ... };
Plack::Runner->run('--app' => $app, @ARGV);
=head1 DESCRIPTION
Plack::Runner is the core of L<plackup> runner script. You can create
your own frontend to run your application or framework, munge command
line options and pass that to C<run> method of this class.
C<run> method does exactly the same thing as the L<plackup> script
does, but one notable addition is that you can pass a PSGI application
code reference directly with C<--app> option, rather than via C<.psgi>
file path or with C<-e> switch. This would be useful if you want to
make an installable PSGI application.
Also, when C<-h> or C<--help> switch is passed, the usage text is
automatically extracted from your own script using L<Pod::Usage>.
=head1 NOTES
Do not directly call this module from your C<.psgi>, since that makes
your PSGI application unnecesarily depend on L<plackup> and won't run
other backends like L<Plack::Server::Apache2> or mod_psgi.
If you I<really> want to make your C<.psgi> runnable as a standalone
script, you can do this:
# foo.psgi
if (__FILE__ eq $0) {
require Plack::Runner;
Plack::Runner->run(@ARGV, $0);
}
# This should always come last
my $app = sub { ... };
=head1 SEE ALSO
L<plackup>
=cut