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