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