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