added Deploy prototype
[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',
2e81e132 23 documentation => qq{
57dc50b0 24 -d --debug force debug mode
2e81e132 25 }
f59a9d1b 26
27);
28
57dc50b0 29has help => (
e46bf32c 30 traits => [qw(Getopt)],
90b481b1 31 cmd_aliases => 'h',
57dc50b0 32 isa => 'Bool',
33 is => 'ro',
2e81e132 34 documentation => qq{
57dc50b0 35 -h --help display this help and exits
36 },
e46bf32c 37);
38
57dc50b0 39has host => (
40 isa => 'Str',
41 is => 'ro',
42 ,
43 default => "localhost"
41b55019 44);
90b481b1 45
57dc50b0 46has fork => (
90b481b1 47 traits => [qw(Getopt)],
48 cmd_aliases => 'f',
49 isa => 'Bool',
57dc50b0 50 is => 'ro',
51
90b481b1 52);
53
57dc50b0 54has listen => (
204c6935 55 traits => [qw(Getopt)],
56 cmd_aliases => 'l',
57 isa => 'Int',
57dc50b0 58 is => 'ro',
59 ,
60 default => "3000"
204c6935 61);
62
57dc50b0 63has pidfile => (
41b55019 64 traits => [qw(Getopt)],
b6aaf142 65 cmd_aliases => 'pid',
57dc50b0 66 isa => 'Str',
67 is => 'ro',
68
41b55019 69);
70
57dc50b0 71has keepalive => (
bd31e11f 72 traits => [qw(Getopt)],
73 cmd_aliases => 'k',
57dc50b0 74 isa => 'Bool',
75 is => 'ro',
76 ,
77
bd31e11f 78);
79
57dc50b0 80has background => (
3954573c 81 traits => [qw(Getopt)],
82 cmd_aliases => 'bg',
57dc50b0 83 isa => 'Bool',
84 is => 'ro',
3954573c 85);
86
4ebd5ecf 87
57dc50b0 88has _app => (
89 reader => 'app',
80a909c2 90 init_arg => 'app',
91 traits => [qw(NoGetopt)],
57dc50b0 92 isa => 'Str',
93 is => 'ro',
94);
4b3881d4 95
8f01ed5f 96has restart => (
97 traits => [qw(Getopt)],
57dc50b0 98 cmd_aliases => 'r',
99 isa => 'Bool',
100 is => 'ro',
101
102);
a7dea640 103
104has restart_directory => (
105 traits => [qw(Getopt)],
106 cmd_aliases => 'rdir',
abee32cb 107 isa => 'ArrayRef[Str]',
a7dea640 108 is => 'ro',
abee32cb 109 predicate => '_has_restart_directory',
8f01ed5f 110);
111
57dc50b0 112has restart_delay => (
70871584 113 traits => [qw(Getopt)],
114 cmd_aliases => 'rdel',
57dc50b0 115 isa => 'Int',
116 is => 'ro',
abee32cb 117 predicate => '_has_restart_delay',
70871584 118);
119
57dc50b0 120has restart_regex => (
ee7aabd6 121 traits => [qw(Getopt)],
122 cmd_aliases => 'rxp',
57dc50b0 123 isa => 'Str',
124 is => 'ro',
abee32cb 125 predicate => '_has_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',
57dc50b0 134
bbd42ac8 135);
a3ca4468 136
8bae939b 137sub usage {
138 my ($self) = shift;
57dc50b0 139
8bae939b 140 return pod2usage();
141
142}
143
a3ca4468 144
145sub run {
d1014540 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
abee32cb 154 # If we load this here, then in the case of a restarter, it does not
155 # need to be reloaded for each restart.
156 require Catalyst;
157
158 # If this isn't done, then the Catalyst::Devel tests for the restarter
159 # fail.
160 $| = 1 if $ENV{HARNESS_ACTIVE};
161
a7dea640 162 if ( $self->restart ) {
163 die "Cannot run in the background and also watch for changed files.\n"
164 if $self->background;
165
166 require Catalyst::Restarter;
167
168 my $subclass = Catalyst::Restarter->pick_subclass;
169
170 my %args;
abee32cb 171 $args{follow_symlinks} = $self->follow_symlinks
172 if $self->_has_follow_symlinks;
173 $args{directories} = $self->restart_directory
174 if $self->_has_restart_directory;
175 $args{sleep_interval} = $self->restart_delay
176 if $self->_has_restart_delay;
177 $args{filter} = qr/$self->restart_regex/
178 if $self->_has_restart_regex;
a7dea640 179
180 my $restarter = $subclass->new(
181 %args,
abee32cb 182 start_sub => sub { $self->_run },
d1014540 183 argv => \$self->ARGV,
a7dea640 184 );
185
186 $restarter->run_and_watch;
187 }
188 else {
abee32cb 189 $self->_run;
a7dea640 190 }
191
192
57dc50b0 193}
194
abee32cb 195sub _run {
a7dea640 196 my ($self) = shift;
57dc50b0 197
a3ca4468 198 my $app = $self->app;
199 Class::MOP::load_class($app);
a7dea640 200
a3ca4468 201 $app->run(
202 $self->listen, $self->host,
57dc50b0 203 {
4b3881d4 204 'fork' => $self->fork,
205 keepalive => $self->keepalive,
206 background => $self->background,
207 pidfile => $self->pidfile,
615fee7c 208 keepalive => $self->keepalive,
615fee7c 209 follow_symlinks => $self->follow_symlinks,
57dc50b0 210 }
a3ca4468 211 );
a3ca4468 212}
5b923b0b 213
a7dea640 214
2e81e132 215no Moose;
216__PACKAGE__->meta->make_immutable;
5b923b0b 217
0ba6e8aa 2181;
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
257Run a Catalyst Testserver for this application.
258
259=head1 AUTHORS
260
261Catalyst Contributors, see Catalyst.pm
262
263=head1 COPYRIGHT
264
265This library is free software. You can redistribute it and/or modify
266it under the same terms as Perl itself.
267
268=cut