fasterized
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / Server.pm
CommitLineData
0ba6e8aa 1package Catalyst::Script::Server;
2
53b08cf9 3BEGIN {
4 $ENV{CATALYST_ENGINE} ||= 'HTTP';
5 $ENV{CATALYST_SCRIPT_GEN} = 31;
6 require Catalyst::Engine::HTTP;
7}
5b923b0b 8
a3ca4468 9use FindBin qw/$Bin/;
10use lib "$Bin/../lib";
11use Pod::Usage;
12use Moose;
a7dea640 13use Catalyst::Restarter;
59804176 14use namespace::autoclean;
a3ca4468 15
16with 'MooseX::Getopt';
17
f59a9d1b 18has debug => (
19 traits => [qw(Getopt)],
20 cmd_aliases => 'd',
57dc50b0 21 isa => 'Bool',
f59a9d1b 22 is => 'ro',
70dc1717 23 documentation => qq{ force debug mode }
f59a9d1b 24
25);
26
57dc50b0 27has help => (
e46bf32c 28 traits => [qw(Getopt)],
90b481b1 29 cmd_aliases => 'h',
57dc50b0 30 isa => 'Bool',
31 is => 'ro',
70dc1717 32 documentation => qq{ display this help and exits },
e46bf32c 33);
34
57dc50b0 35has host => (
36 isa => 'Str',
37 is => 'ro',
70dc1717 38 default => "localhost",
39 documentation => qq{ specify a host for the server to run on }
41b55019 40);
90b481b1 41
57dc50b0 42has fork => (
90b481b1 43 traits => [qw(Getopt)],
44 cmd_aliases => 'f',
45 isa => 'Bool',
57dc50b0 46 is => 'ro',
70dc1717 47 documentation => qq{ fork the server }
90b481b1 48);
49
57dc50b0 50has listen => (
204c6935 51 traits => [qw(Getopt)],
52 cmd_aliases => 'l',
53 isa => 'Int',
57dc50b0 54 is => 'ro',
70dc1717 55 default => "3000",
56 documentation => qq{ specify a different listening port }
204c6935 57);
58
57dc50b0 59has pidfile => (
41b55019 60 traits => [qw(Getopt)],
b6aaf142 61 cmd_aliases => 'pid',
57dc50b0 62 isa => 'Str',
63 is => 'ro',
70dc1717 64 documentation => qq{ specify a pidfile }
41b55019 65);
66
57dc50b0 67has keepalive => (
bd31e11f 68 traits => [qw(Getopt)],
69 cmd_aliases => 'k',
57dc50b0 70 isa => 'Bool',
71 is => 'ro',
70dc1717 72 documentation => qq{ server keepalive },
57dc50b0 73
bd31e11f 74);
75
57dc50b0 76has background => (
3954573c 77 traits => [qw(Getopt)],
78 cmd_aliases => 'bg',
57dc50b0 79 isa => 'Bool',
80 is => 'ro',
70dc1717 81 documentation => qq{ run in the background }
3954573c 82);
83
4ebd5ecf 84
57dc50b0 85has _app => (
86 reader => 'app',
80a909c2 87 init_arg => 'app',
88 traits => [qw(NoGetopt)],
57dc50b0 89 isa => 'Str',
90 is => 'ro',
91);
4b3881d4 92
8f01ed5f 93has restart => (
94 traits => [qw(Getopt)],
57dc50b0 95 cmd_aliases => 'r',
96 isa => 'Bool',
97 is => 'ro',
70dc1717 98 documentation => qq{ use Catalyst::Restarter to detect code changes }
57dc50b0 99);
a7dea640 100
101has 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 110has restart_delay => (
70871584 111 traits => [qw(Getopt)],
112 cmd_aliases => 'rdel',
57dc50b0 113 isa => 'Int',
114 is => 'ro',
abee32cb 115 predicate => '_has_restart_delay',
70dc1717 116 documentation => qq{ set a restart delay }
70871584 117);
118
57dc50b0 119has restart_regex => (
ee7aabd6 120 traits => [qw(Getopt)],
121 cmd_aliases => 'rxp',
57dc50b0 122 isa => 'Str',
123 is => 'ro',
abee32cb 124 predicate => '_has_restart_regex',
70dc1717 125 documentation => qq{ restart regex }
ee7aabd6 126);
127
57dc50b0 128has follow_symlinks => (
bbd42ac8 129 traits => [qw(Getopt)],
130 cmd_aliases => 'sym',
57dc50b0 131 isa => 'Bool',
132 is => 'ro',
abee32cb 133 predicate => '_has_follow_symlinks',
70dc1717 134 documentation => qq{ follow symbolic links }
57dc50b0 135
bbd42ac8 136);
a3ca4468 137
8bae939b 138sub usage {
139 my ($self) = shift;
57dc50b0 140
8bae939b 141 return pod2usage();
142
143}
144
a3ca4468 145
146sub 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 196sub _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 216no Moose;
217__PACKAGE__->meta->make_immutable;
5b923b0b 218
0ba6e8aa 2191;
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
258Run a Catalyst Testserver for this application.
259
260=head1 AUTHORS
261
262Catalyst Contributors, see Catalyst.pm
263
264=head1 COPYRIGHT
265
266This library is free software. You can redistribute it and/or modify
267it under the same terms as Perl itself.
268
269=cut