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