added MX::Types::Moose
[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;
4e45780e 14use MooseX::Types::Moose qw/Str Bool Int/;
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',
70dc1717 24 documentation => qq{ force debug mode }
f59a9d1b 25
26);
27
57dc50b0 28has help => (
e46bf32c 29 traits => [qw(Getopt)],
90b481b1 30 cmd_aliases => 'h',
57dc50b0 31 isa => 'Bool',
32 is => 'ro',
70dc1717 33 documentation => qq{ display this help and exits },
e46bf32c 34);
35
57dc50b0 36has host => (
37 isa => 'Str',
38 is => 'ro',
70dc1717 39 default => "localhost",
40 documentation => qq{ specify a host for the server to run on }
41b55019 41);
90b481b1 42
57dc50b0 43has fork => (
90b481b1 44 traits => [qw(Getopt)],
45 cmd_aliases => 'f',
46 isa => 'Bool',
57dc50b0 47 is => 'ro',
70dc1717 48 documentation => qq{ fork the server }
90b481b1 49);
50
57dc50b0 51has listen => (
204c6935 52 traits => [qw(Getopt)],
53 cmd_aliases => 'l',
54 isa => 'Int',
57dc50b0 55 is => 'ro',
70dc1717 56 default => "3000",
57 documentation => qq{ specify a different listening port }
204c6935 58);
59
57dc50b0 60has pidfile => (
41b55019 61 traits => [qw(Getopt)],
b6aaf142 62 cmd_aliases => 'pid',
57dc50b0 63 isa => 'Str',
64 is => 'ro',
70dc1717 65 documentation => qq{ specify a pidfile }
41b55019 66);
67
57dc50b0 68has keepalive => (
bd31e11f 69 traits => [qw(Getopt)],
70 cmd_aliases => 'k',
57dc50b0 71 isa => 'Bool',
72 is => 'ro',
70dc1717 73 documentation => qq{ server keepalive },
57dc50b0 74
bd31e11f 75);
76
57dc50b0 77has background => (
3954573c 78 traits => [qw(Getopt)],
79 cmd_aliases => 'bg',
57dc50b0 80 isa => 'Bool',
81 is => 'ro',
70dc1717 82 documentation => qq{ run in the background }
3954573c 83);
84
4ebd5ecf 85
57dc50b0 86has _app => (
87 reader => 'app',
80a909c2 88 init_arg => 'app',
89 traits => [qw(NoGetopt)],
57dc50b0 90 isa => 'Str',
91 is => 'ro',
92);
4b3881d4 93
8f01ed5f 94has restart => (
95 traits => [qw(Getopt)],
57dc50b0 96 cmd_aliases => 'r',
97 isa => 'Bool',
98 is => 'ro',
70dc1717 99 documentation => qq{ use Catalyst::Restarter to detect code changes }
57dc50b0 100);
a7dea640 101
102has 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 111has restart_delay => (
70871584 112 traits => [qw(Getopt)],
113 cmd_aliases => 'rdel',
57dc50b0 114 isa => 'Int',
115 is => 'ro',
abee32cb 116 predicate => '_has_restart_delay',
70dc1717 117 documentation => qq{ set a 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',
70dc1717 126 documentation => qq{ restart regex }
ee7aabd6 127);
128
57dc50b0 129has follow_symlinks => (
bbd42ac8 130 traits => [qw(Getopt)],
131 cmd_aliases => 'sym',
57dc50b0 132 isa => 'Bool',
133 is => 'ro',
abee32cb 134 predicate => '_has_follow_symlinks',
70dc1717 135 documentation => qq{ follow symbolic links }
57dc50b0 136
bbd42ac8 137);
a3ca4468 138
8bae939b 139sub usage {
140 my ($self) = shift;
57dc50b0 141
8bae939b 142 return pod2usage();
143
144}
145
a3ca4468 146
147sub 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 197sub _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 217no Moose;
218__PACKAGE__->meta->make_immutable;
5b923b0b 219
0ba6e8aa 2201;
e46bf32c 221
222=head1 NAME
223
224[% appprefix %]_server.pl - Catalyst Testserver
225
226=head1 SYNOPSIS
227
228[% appprefix %]_server.pl [options]
229
230 Options:
4b3881d4 231 -d --debug force debug mode
232 -f --fork handle each request in a new process
e46bf32c 233 (defaults to false)
4b3881d4 234 -h --help display this help and exits
235 --host host (defaults to all)
236 -p --port port (defaults to 3000)
237 -k --keepalive enable keep-alive connections
238 -r --restart restart when files get modified
239 (defaults to false)
240 --rd --restartdelay delay between file checks
e46bf32c 241 (ignored if you have Linux::Inotify2 installed)
4b3881d4 242 --rr --restartregex regex match files that trigger
e46bf32c 243 a restart when modified
244 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
4b3881d4 245 --rdir --restartdirectory the directory to search for
e46bf32c 246 modified files, can be set mulitple times
247 (defaults to '[SCRIPT_DIR]/..')
4b3881d4 248 --sym --follow_symlinks follow symlinks in search directories
e46bf32c 249 (defaults to false. this is a no-op on Win32)
4b3881d4 250 --bg --background run the process in the background
251 --pid --pidfile specify filename for pid file
e46bf32c 252
253 See also:
254 perldoc Catalyst::Manual
255 perldoc Catalyst::Manual::Intro
256
257=head1 DESCRIPTION
258
259Run a Catalyst Testserver for this application.
260
261=head1 AUTHORS
262
263Catalyst Contributors, see Catalyst.pm
264
265=head1 COPYRIGHT
266
267This library is free software. You can redistribute it and/or modify
268it under the same terms as Perl itself.
269
270=cut