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