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