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