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