Commit | Line | Data |
0ba6e8aa |
1 | package Catalyst::Script::Server; |
a3ca4468 |
2 | use Moose; |
b89d8b98 |
3 | use Catalyst::Utils; |
fa38321d |
4 | use Try::Tiny; |
59804176 |
5 | use namespace::autoclean; |
a3ca4468 |
6 | |
d3082fac |
7 | with 'Catalyst::ScriptRole'; |
a3ca4468 |
8 | |
f59a9d1b |
9 | has debug => ( |
4387c692 |
10 | traits => [qw(Getopt)], |
11 | cmd_aliases => 'd', |
8c848c33 |
12 | isa => 'Bool', |
4387c692 |
13 | is => 'ro', |
d3082fac |
14 | documentation => q{Force debug mode}, |
e46bf32c |
15 | ); |
16 | |
57dc50b0 |
17 | has host => ( |
4387c692 |
18 | traits => [qw(Getopt)], |
19 | cmd_aliases => 'h', |
8c848c33 |
20 | isa => 'Str', |
4387c692 |
21 | is => 'ro', |
b1bfeea6 |
22 | # N.B. undef (the default) means we bind on all interfaces on the host. |
3bd410a9 |
23 | documentation => 'Specify a hostname or IP on this host for the server to bind to', |
41b55019 |
24 | ); |
90b481b1 |
25 | |
57dc50b0 |
26 | has fork => ( |
4387c692 |
27 | traits => [qw(Getopt)], |
28 | cmd_aliases => 'f', |
8c848c33 |
29 | isa => 'Bool', |
4387c692 |
30 | is => 'ro', |
31 | default => 0, |
53c6ec79 |
32 | documentation => 'Fork the server to be able to serve multiple requests at once', |
90b481b1 |
33 | ); |
34 | |
bc6fa9fc |
35 | has port => ( |
4387c692 |
36 | traits => [qw(Getopt)], |
37 | cmd_aliases => 'p', |
8c848c33 |
38 | isa => 'Int', |
4387c692 |
39 | is => 'ro', |
b89d8b98 |
40 | default => sub { |
56eb9db4 |
41 | Catalyst::Utils::env_value(shift->application_name, 'port') || 3000 |
b89d8b98 |
42 | }, |
53c6ec79 |
43 | documentation => 'Specify a different listening port (to the default port 3000)', |
204c6935 |
44 | ); |
45 | |
a6878cd8 |
46 | use Moose::Util::TypeConstraints; |
47 | class_type 'MooseX::Daemonize::Pid::File'; |
fa38321d |
48 | subtype 'Catalyst::Script::Server::Types::Pidfile', |
51e2ff98 |
49 | as 'MooseX::Daemonize::Pid::File'; |
50 | |
8c848c33 |
51 | coerce 'Catalyst::Script::Server::Types::Pidfile', from 'Str', via { |
fa38321d |
52 | try { Class::MOP::load_class("MooseX::Daemonize::Pid::File") } |
53 | catch { |
54 | warn("Could not load MooseX::Daemonize::Pid::File, needed for --pid option\n"); |
55 | exit 1; |
56 | }; |
a6878cd8 |
57 | MooseX::Daemonize::Pid::File->new( file => $_ ); |
58 | }; |
59 | MooseX::Getopt::OptionTypeMap->add_option_type_to_map( |
fa38321d |
60 | 'Catalyst::Script::Server::Types::Pidfile' => '=s', |
a6878cd8 |
61 | ); |
57dc50b0 |
62 | has pidfile => ( |
4387c692 |
63 | traits => [qw(Getopt)], |
64 | cmd_aliases => 'pid', |
fa38321d |
65 | isa => 'Catalyst::Script::Server::Types::Pidfile', |
4387c692 |
66 | is => 'ro', |
d3082fac |
67 | documentation => 'Specify a pidfile', |
a6878cd8 |
68 | coerce => 1, |
69 | predicate => '_has_pidfile', |
41b55019 |
70 | ); |
71 | |
df894348 |
72 | # Override MooseX::Daemonize |
73 | sub dont_close_all_files { 1 } |
a6878cd8 |
74 | sub BUILD { |
75 | my $self = shift; |
00064198 |
76 | |
07b56dc9 |
77 | if ($self->background) { |
78 | # FIXME - This is evil. Should we just add MX::Daemonize to the deps? |
df894348 |
79 | try { Class::MOP::load_class('MooseX::Daemonize::Core'); Class::MOP::load_class('POSIX') } |
fa38321d |
80 | catch { |
81 | warn("MooseX::Daemonize is needed for the --background option\n"); |
82 | exit 1; |
83 | }; |
07b56dc9 |
84 | MooseX::Daemonize::Core->meta->apply($self); |
df894348 |
85 | POSIX::close($_) foreach (0..2); |
07b56dc9 |
86 | } |
a6878cd8 |
87 | } |
88 | |
57dc50b0 |
89 | has keepalive => ( |
4387c692 |
90 | traits => [qw(Getopt)], |
91 | cmd_aliases => 'k', |
8c848c33 |
92 | isa => 'Bool', |
4387c692 |
93 | is => 'ro', |
94 | default => 0, |
53c6ec79 |
95 | documentation => 'Support keepalive', |
bd31e11f |
96 | ); |
97 | |
57dc50b0 |
98 | has background => ( |
4387c692 |
99 | traits => [qw(Getopt)], |
100 | cmd_aliases => 'bg', |
8c848c33 |
101 | isa => 'Bool', |
4387c692 |
102 | is => 'ro', |
103 | default => 0, |
d3082fac |
104 | documentation => 'Run in the background', |
57dc50b0 |
105 | ); |
4b3881d4 |
106 | |
8f01ed5f |
107 | has restart => ( |
4387c692 |
108 | traits => [qw(Getopt)], |
109 | cmd_aliases => 'r', |
8c848c33 |
110 | isa => 'Bool', |
4387c692 |
111 | is => 'ro', |
56eb9db4 |
112 | default => sub { |
113 | Catalyst::Utils::env_value(shift->application_name, 'reload') || 0; |
114 | }, |
53c6ec79 |
115 | documentation => 'use Catalyst::Restarter to detect code changes and restart the application', |
57dc50b0 |
116 | ); |
a7dea640 |
117 | |
118 | has restart_directory => ( |
4387c692 |
119 | traits => [qw(Getopt)], |
1c517146 |
120 | cmd_aliases => [ 'rdir', 'restartdirectory' ], |
8c848c33 |
121 | isa => 'ArrayRef[Str]', |
4387c692 |
122 | is => 'ro', |
d3082fac |
123 | documentation => 'Restarter directory to watch', |
4387c692 |
124 | predicate => '_has_restart_directory', |
8f01ed5f |
125 | ); |
126 | |
57dc50b0 |
127 | has restart_delay => ( |
4387c692 |
128 | traits => [qw(Getopt)], |
129 | cmd_aliases => 'rd', |
8c848c33 |
130 | isa => 'Int', |
4387c692 |
131 | is => 'ro', |
d3082fac |
132 | documentation => 'Set a restart delay', |
4387c692 |
133 | predicate => '_has_restart_delay', |
70871584 |
134 | ); |
135 | |
880d1f8b |
136 | { |
137 | use Moose::Util::TypeConstraints; |
138 | |
8c848c33 |
139 | my $tc = subtype 'Catalyst::Script::Server::Types::RegexpRef', as 'RegexpRef'; |
140 | coerce $tc, from 'Str', via { qr/$_/ }; |
880d1f8b |
141 | |
142 | MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s'); |
143 | |
144 | has restart_regex => ( |
4387c692 |
145 | traits => [qw(Getopt)], |
146 | cmd_aliases => 'rr', |
147 | isa => $tc, |
148 | coerce => 1, |
149 | is => 'ro', |
880d1f8b |
150 | documentation => 'Restart regex', |
4387c692 |
151 | predicate => '_has_restart_regex', |
880d1f8b |
152 | ); |
153 | } |
ee7aabd6 |
154 | |
57dc50b0 |
155 | has follow_symlinks => ( |
4387c692 |
156 | traits => [qw(Getopt)], |
157 | cmd_aliases => 'sym', |
8c848c33 |
158 | isa => 'Bool', |
4387c692 |
159 | is => 'ro', |
160 | default => 0, |
d3082fac |
161 | documentation => 'Follow symbolic links', |
4387c692 |
162 | predicate => '_has_follow_symlinks', |
bbd42ac8 |
163 | ); |
a3ca4468 |
164 | |
a6878cd8 |
165 | sub _plack_engine_name { |
166 | my $self = shift; |
0cca6153 |
167 | return $self->fork || $self->keepalive ? 'Starman' : 'Standalone'; |
a6878cd8 |
168 | } |
169 | |
3453b929 |
170 | sub _restarter_args { |
171 | my $self = shift; |
34be7d45 |
172 | |
10255473 |
173 | return ( |
34be7d45 |
174 | argv => $self->ARGV, |
175 | start_sub => sub { $self->_run_application }, |
176 | ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()), |
177 | ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()), |
178 | ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()), |
880d1f8b |
179 | ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()), |
a024e9ad |
180 | ), |
181 | ( |
8dde82d5 |
182 | map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive) |
34be7d45 |
183 | ); |
3453b929 |
184 | } |
185 | |
75fe0bb3 |
186 | has restarter_class => ( |
187 | is => 'ro', |
8c848c33 |
188 | isa => 'Str', |
75fe0bb3 |
189 | lazy => 1, |
190 | default => sub { |
191 | my $self = shift; |
192 | Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter'; |
193 | } |
194 | ); |
195 | |
a3ca4468 |
196 | sub run { |
3453b929 |
197 | my $self = shift; |
57dc50b0 |
198 | |
53c6ec79 |
199 | local $ENV{CATALYST_DEBUG} = 1 |
200 | if $self->debug; |
abee32cb |
201 | |
a7dea640 |
202 | if ( $self->restart ) { |
203 | die "Cannot run in the background and also watch for changed files.\n" |
204 | if $self->background; |
00064198 |
205 | die "Cannot write out a pid file and fork for the restarter.\n" |
206 | if $self->_has_pidfile; |
a7dea640 |
207 | |
53c6ec79 |
208 | # If we load this here, then in the case of a restarter, it does not |
209 | # need to be reloaded for each restart. |
210 | require Catalyst; |
211 | |
212 | # If this isn't done, then the Catalyst::Devel tests for the restarter |
213 | # fail. |
214 | $| = 1 if $ENV{HARNESS_ACTIVE}; |
215 | |
a024e9ad |
216 | Catalyst::Utils::ensure_class_loaded($self->restarter_class); |
a7dea640 |
217 | |
75fe0bb3 |
218 | my $subclass = $self->restarter_class->pick_subclass; |
a7dea640 |
219 | |
a7dea640 |
220 | my $restarter = $subclass->new( |
3453b929 |
221 | $self->_restarter_args() |
a7dea640 |
222 | ); |
223 | |
224 | $restarter->run_and_watch; |
225 | } |
226 | else { |
07b56dc9 |
227 | if ($self->background) { |
228 | $self->daemon_fork; |
229 | |
230 | return 1 unless $self->is_daemon; |
231 | |
232 | Class::MOP::load_class($self->application_name); |
233 | |
234 | $self->daemon_detach; |
235 | } |
236 | |
00064198 |
237 | $self->pidfile->write |
238 | if $self->_has_pidfile; |
239 | |
d3082fac |
240 | $self->_run_application; |
a7dea640 |
241 | } |
242 | |
243 | |
57dc50b0 |
244 | } |
245 | |
36a53c3a |
246 | sub _plack_loader_args { |
247 | my ($self) = shift; |
248 | return ( |
249 | port => $self->port, |
250 | host => $self->host, |
251 | keepalive => $self->keepalive ? 100 : 1, |
54ab9446 |
252 | server_ready => sub { |
253 | my ($args) = @_; |
254 | |
255 | my $name = $args->{server_software} || ref($args); # $args is $server |
256 | my $host = $args->{host} || 0; |
257 | my $proto = $args->{proto} || 'http'; |
258 | |
259 | print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n"; |
260 | }, |
36a53c3a |
261 | ); |
262 | } |
263 | |
aee7cdcc |
264 | around _application_args => sub { |
265 | my ($orig, $self) = @_; |
d3082fac |
266 | return ( |
bc6fa9fc |
267 | $self->port, |
d3082fac |
268 | $self->host, |
57dc50b0 |
269 | { |
aee7cdcc |
270 | %{ $self->$orig }, |
d3082fac |
271 | map { $_ => $self->$_ } qw/ |
272 | fork |
273 | keepalive |
274 | background |
275 | pidfile |
276 | keepalive |
277 | follow_symlinks |
aee7cdcc |
278 | port |
279 | host |
d3082fac |
280 | /, |
281 | }, |
a3ca4468 |
282 | ); |
aee7cdcc |
283 | }; |
5b923b0b |
284 | |
2e81e132 |
285 | __PACKAGE__->meta->make_immutable; |
0ba6e8aa |
286 | 1; |
e46bf32c |
287 | |
288 | =head1 NAME |
289 | |
cbaaecc0 |
290 | Catalyst::Script::Server - Catalyst test server |
e46bf32c |
291 | |
292 | =head1 SYNOPSIS |
293 | |
cbaaecc0 |
294 | myapp_server.pl [options] |
e46bf32c |
295 | |
296 | Options: |
4b3881d4 |
297 | -d --debug force debug mode |
298 | -f --fork handle each request in a new process |
e46bf32c |
299 | (defaults to false) |
3dcfb4c8 |
300 | --help display this help and exits |
301 | -h --host host (defaults to all) |
4b3881d4 |
302 | -p --port port (defaults to 3000) |
303 | -k --keepalive enable keep-alive connections |
304 | -r --restart restart when files get modified |
305 | (defaults to false) |
87f5a448 |
306 | --rd --restart_delay delay between file checks |
e46bf32c |
307 | (ignored if you have Linux::Inotify2 installed) |
87f5a448 |
308 | --rr --restart_regex regex match files that trigger |
e46bf32c |
309 | a restart when modified |
310 | (defaults to '\.yml$|\.yaml$|\.conf|\.pm$') |
87f5a448 |
311 | --rdir --restart_directory the directory to search for |
6ab9f4af |
312 | modified files, can be set multiple times |
e46bf32c |
313 | (defaults to '[SCRIPT_DIR]/..') |
4b3881d4 |
314 | --sym --follow_symlinks follow symlinks in search directories |
e46bf32c |
315 | (defaults to false. this is a no-op on Win32) |
4b3881d4 |
316 | --bg --background run the process in the background |
317 | --pid --pidfile specify filename for pid file |
e46bf32c |
318 | |
319 | See also: |
320 | perldoc Catalyst::Manual |
321 | perldoc Catalyst::Manual::Intro |
322 | |
323 | =head1 DESCRIPTION |
324 | |
cbaaecc0 |
325 | Run a Catalyst test server for this application. |
e46bf32c |
326 | |
383c5be6 |
327 | =head1 SEE ALSO |
328 | |
329 | L<Catalyst::ScriptRunner> |
330 | |
e46bf32c |
331 | =head1 AUTHORS |
332 | |
333 | Catalyst Contributors, see Catalyst.pm |
334 | |
335 | =head1 COPYRIGHT |
336 | |
337 | This library is free software. You can redistribute it and/or modify |
338 | it under the same terms as Perl itself. |
339 | |
340 | =cut |