Merge 'trunk' into 'better_scripts'
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / Server.pm
CommitLineData
0ba6e8aa 1package Catalyst::Script::Server;
2
53b08cf9 3BEGIN {
4 $ENV{CATALYST_ENGINE} ||= 'HTTP';
53b08cf9 5 require Catalyst::Engine::HTTP;
6}
5b923b0b 7
a3ca4468 8use Moose;
d3082fac 9use MooseX::Types::Moose qw/ArrayRef Str Bool Int/;
59804176 10use namespace::autoclean;
a3ca4468 11
d3082fac 12with 'Catalyst::ScriptRole';
a3ca4468 13
3a8c155f 14__PACKAGE__->meta->get_attribute('help')->cmd_aliases('?');
15
f59a9d1b 16has debug => (
ab7eb5a9 17 traits => [qw(Getopt)],
f59a9d1b 18 cmd_aliases => 'd',
73e4f0f1 19 isa => Bool,
f59a9d1b 20 is => 'ro',
d3082fac 21 documentation => q{Force debug mode},
e46bf32c 22);
23
57dc50b0 24has host => (
ab7eb5a9 25 traits => [qw(Getopt)],
53c6ec79 26 cmd_aliases => 'h',
73e4f0f1 27 isa => Str,
57dc50b0 28 is => 'ro',
d3082fac 29 default => 'localhost',
53c6ec79 30 documentation => 'Specify an IP on this host for the server to bind to',
41b55019 31);
90b481b1 32
57dc50b0 33has fork => (
ab7eb5a9 34 traits => [qw(Getopt)],
90b481b1 35 cmd_aliases => 'f',
73e4f0f1 36 isa => Bool,
57dc50b0 37 is => 'ro',
53c6ec79 38 default => 0,
39 documentation => 'Fork the server to be able to serve multiple requests at once',
90b481b1 40);
41
bc6fa9fc 42has port => (
ab7eb5a9 43 traits => [qw(Getopt)],
bc6fa9fc 44 cmd_aliases => 'p',
73e4f0f1 45 isa => Int,
57dc50b0 46 is => 'ro',
d3082fac 47 default => 3000,
53c6ec79 48 documentation => 'Specify a different listening port (to the default port 3000)',
204c6935 49);
50
57dc50b0 51has pidfile => (
ab7eb5a9 52 traits => [qw(Getopt)],
b6aaf142 53 cmd_aliases => 'pid',
73e4f0f1 54 isa => Str,
57dc50b0 55 is => 'ro',
d3082fac 56 documentation => 'Specify a pidfile',
41b55019 57);
58
57dc50b0 59has keepalive => (
ab7eb5a9 60 traits => [qw(Getopt)],
bd31e11f 61 cmd_aliases => 'k',
73e4f0f1 62 isa => Bool,
57dc50b0 63 is => 'ro',
53c6ec79 64 default => 0,
65 documentation => 'Support keepalive',
bd31e11f 66);
67
57dc50b0 68has background => (
ab7eb5a9 69 traits => [qw(Getopt)],
3954573c 70 cmd_aliases => 'bg',
73e4f0f1 71 isa => Bool,
57dc50b0 72 is => 'ro',
53c6ec79 73 default => 0,
d3082fac 74 documentation => 'Run in the background',
57dc50b0 75);
4b3881d4 76
8f01ed5f 77has restart => (
ab7eb5a9 78 traits => [qw(Getopt)],
57dc50b0 79 cmd_aliases => 'r',
73e4f0f1 80 isa => Bool,
57dc50b0 81 is => 'ro',
53c6ec79 82 default => 0,
83 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
57dc50b0 84);
a7dea640 85
86has restart_directory => (
ab7eb5a9 87 traits => [qw(Getopt)],
a7dea640 88 cmd_aliases => 'rdir',
d3082fac 89 isa => ArrayRef[Str],
a7dea640 90 is => 'ro',
d3082fac 91 documentation => 'Restarter directory to watch',
53c6ec79 92 predicate => '_has_restart_directory',
8f01ed5f 93);
94
57dc50b0 95has restart_delay => (
ab7eb5a9 96 traits => [qw(Getopt)],
53c6ec79 97 cmd_aliases => 'rd',
73e4f0f1 98 isa => Int,
57dc50b0 99 is => 'ro',
d3082fac 100 documentation => 'Set a restart delay',
53c6ec79 101 predicate => '_has_restart_delay',
70871584 102);
103
57dc50b0 104has restart_regex => (
ee7aabd6 105 traits => [qw(Getopt)],
53c6ec79 106 cmd_aliases => 'rr',
73e4f0f1 107 isa => Str,
57dc50b0 108 is => 'ro',
d3082fac 109 documentation => 'Restart regex',
53c6ec79 110 predicate => '_has_restart_regex',
ee7aabd6 111);
112
57dc50b0 113has follow_symlinks => (
bbd42ac8 114 traits => [qw(Getopt)],
115 cmd_aliases => 'sym',
73e4f0f1 116 isa => Bool,
57dc50b0 117 is => 'ro',
53c6ec79 118 default => 0,
d3082fac 119 documentation => 'Follow symbolic links',
bbd42ac8 120);
a3ca4468 121
a3ca4468 122sub run {
d1014540 123 my ($self) = shift;
57dc50b0 124
53c6ec79 125 local $ENV{CATALYST_DEBUG} = 1
126 if $self->debug;
abee32cb 127
a7dea640 128 if ( $self->restart ) {
129 die "Cannot run in the background and also watch for changed files.\n"
130 if $self->background;
131
53c6ec79 132 # If we load this here, then in the case of a restarter, it does not
133 # need to be reloaded for each restart.
134 require Catalyst;
135
136 # If this isn't done, then the Catalyst::Devel tests for the restarter
137 # fail.
138 $| = 1 if $ENV{HARNESS_ACTIVE};
139
a7dea640 140 require Catalyst::Restarter;
141
142 my $subclass = Catalyst::Restarter->pick_subclass;
143
144 my %args;
abee32cb 145 $args{follow_symlinks} = $self->follow_symlinks
53c6ec79 146 if $self->follow_symlinks;
abee32cb 147 $args{directories} = $self->restart_directory
148 if $self->_has_restart_directory;
149 $args{sleep_interval} = $self->restart_delay
150 if $self->_has_restart_delay;
151 $args{filter} = qr/$self->restart_regex/
152 if $self->_has_restart_regex;
a7dea640 153
154 my $restarter = $subclass->new(
155 %args,
60bfba3f 156 start_sub => sub { $self->_run_application },
157 argv => $self->ARGV,
a7dea640 158 );
159
160 $restarter->run_and_watch;
161 }
162 else {
d3082fac 163 $self->_run_application;
a7dea640 164 }
165
166
57dc50b0 167}
168
d3082fac 169sub _application_args {
a7dea640 170 my ($self) = shift;
d3082fac 171 return (
bc6fa9fc 172 $self->port,
d3082fac 173 $self->host,
57dc50b0 174 {
d3082fac 175 map { $_ => $self->$_ } qw/
176 fork
177 keepalive
178 background
179 pidfile
180 keepalive
181 follow_symlinks
182 /,
183 },
a3ca4468 184 );
a3ca4468 185}
5b923b0b 186
2e81e132 187__PACKAGE__->meta->make_immutable;
5b923b0b 188
0ba6e8aa 1891;
e46bf32c 190
191=head1 NAME
192
cbaaecc0 193Catalyst::Script::Server - Catalyst test server
e46bf32c 194
195=head1 SYNOPSIS
196
cbaaecc0 197 myapp_server.pl [options]
e46bf32c 198
199 Options:
4b3881d4 200 -d --debug force debug mode
201 -f --fork handle each request in a new process
e46bf32c 202 (defaults to false)
4b3881d4 203 -h --help display this help and exits
204 --host host (defaults to all)
205 -p --port port (defaults to 3000)
206 -k --keepalive enable keep-alive connections
207 -r --restart restart when files get modified
208 (defaults to false)
209 --rd --restartdelay delay between file checks
e46bf32c 210 (ignored if you have Linux::Inotify2 installed)
4b3881d4 211 --rr --restartregex regex match files that trigger
e46bf32c 212 a restart when modified
213 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
4b3881d4 214 --rdir --restartdirectory the directory to search for
e46bf32c 215 modified files, can be set mulitple times
216 (defaults to '[SCRIPT_DIR]/..')
4b3881d4 217 --sym --follow_symlinks follow symlinks in search directories
e46bf32c 218 (defaults to false. this is a no-op on Win32)
4b3881d4 219 --bg --background run the process in the background
220 --pid --pidfile specify filename for pid file
e46bf32c 221
222 See also:
223 perldoc Catalyst::Manual
224 perldoc Catalyst::Manual::Intro
225
226=head1 DESCRIPTION
227
cbaaecc0 228Run a Catalyst test server for this application.
e46bf32c 229
230=head1 AUTHORS
231
232Catalyst Contributors, see Catalyst.pm
233
234=head1 COPYRIGHT
235
236This library is free software. You can redistribute it and/or modify
237it under the same terms as Perl itself.
238
239=cut