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