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