Depend on a new CMOP because we rely on its new load_class error messages.
[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
3453b929 122sub _restarter_args {
123 my $self = shift;
124 my %args;
125 $args{follow_symlinks} = $self->follow_symlinks
126 if $self->follow_symlinks;
127 $args{directories} = $self->restart_directory
128 if $self->_has_restart_directory;
129 $args{sleep_interval} = $self->restart_delay
130 if $self->_has_restart_delay;
131 if ($self->_has_restart_regex) {
132 my $regex = $self->restart_regex;
133 $args{filter} = qr/$regex/;
134 }
135 $args{start_sub} = sub { $self->_run_application };
136 $args{argv} = $self->ARGV;
137 return %args;
138}
139
a3ca4468 140sub run {
3453b929 141 my $self = shift;
57dc50b0 142
53c6ec79 143 local $ENV{CATALYST_DEBUG} = 1
144 if $self->debug;
abee32cb 145
a7dea640 146 if ( $self->restart ) {
147 die "Cannot run in the background and also watch for changed files.\n"
148 if $self->background;
149
53c6ec79 150 # If we load this here, then in the case of a restarter, it does not
151 # need to be reloaded for each restart.
152 require Catalyst;
153
154 # If this isn't done, then the Catalyst::Devel tests for the restarter
155 # fail.
156 $| = 1 if $ENV{HARNESS_ACTIVE};
157
a7dea640 158 require Catalyst::Restarter;
159
160 my $subclass = Catalyst::Restarter->pick_subclass;
161
3453b929 162
a7dea640 163 my $restarter = $subclass->new(
3453b929 164 $self->_restarter_args()
a7dea640 165 );
166
167 $restarter->run_and_watch;
168 }
169 else {
d3082fac 170 $self->_run_application;
a7dea640 171 }
172
173
57dc50b0 174}
175
d3082fac 176sub _application_args {
a7dea640 177 my ($self) = shift;
d3082fac 178 return (
bc6fa9fc 179 $self->port,
d3082fac 180 $self->host,
57dc50b0 181 {
d3082fac 182 map { $_ => $self->$_ } qw/
183 fork
184 keepalive
185 background
186 pidfile
187 keepalive
188 follow_symlinks
189 /,
190 },
a3ca4468 191 );
a3ca4468 192}
5b923b0b 193
2e81e132 194__PACKAGE__->meta->make_immutable;
5b923b0b 195
0ba6e8aa 1961;
e46bf32c 197
198=head1 NAME
199
cbaaecc0 200Catalyst::Script::Server - Catalyst test server
e46bf32c 201
202=head1 SYNOPSIS
203
cbaaecc0 204 myapp_server.pl [options]
e46bf32c 205
206 Options:
4b3881d4 207 -d --debug force debug mode
208 -f --fork handle each request in a new process
e46bf32c 209 (defaults to false)
3dcfb4c8 210 --help display this help and exits
211 -h --host host (defaults to all)
4b3881d4 212 -p --port port (defaults to 3000)
213 -k --keepalive enable keep-alive connections
214 -r --restart restart when files get modified
215 (defaults to false)
216 --rd --restartdelay delay between file checks
e46bf32c 217 (ignored if you have Linux::Inotify2 installed)
4b3881d4 218 --rr --restartregex regex match files that trigger
e46bf32c 219 a restart when modified
220 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
4b3881d4 221 --rdir --restartdirectory the directory to search for
e46bf32c 222 modified files, can be set mulitple times
223 (defaults to '[SCRIPT_DIR]/..')
4b3881d4 224 --sym --follow_symlinks follow symlinks in search directories
e46bf32c 225 (defaults to false. this is a no-op on Win32)
4b3881d4 226 --bg --background run the process in the background
227 --pid --pidfile specify filename for pid file
e46bf32c 228
229 See also:
230 perldoc Catalyst::Manual
231 perldoc Catalyst::Manual::Intro
232
233=head1 DESCRIPTION
234
cbaaecc0 235Run a Catalyst test server for this application.
e46bf32c 236
237=head1 AUTHORS
238
239Catalyst Contributors, see Catalyst.pm
240
241=head1 COPYRIGHT
242
243This library is free software. You can redistribute it and/or modify
244it under the same terms as Perl itself.
245
246=cut