Add the crux of options that need fixing from jnap's survey
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / Server.pm
CommitLineData
0ba6e8aa 1package Catalyst::Script::Server;
a3ca4468 2use Moose;
883c37ef 3use MooseX::Types::Common::Numeric qw/PositiveInt/;
880d1f8b 4use MooseX::Types::Moose qw/ArrayRef Str Bool Int RegexpRef/;
b89d8b98 5use Catalyst::Utils;
59804176 6use namespace::autoclean;
a3ca4468 7
c821df21 8sub _plack_engine_name { 'Standalone' }
9
d3082fac 10with 'Catalyst::ScriptRole';
a3ca4468 11
f59a9d1b 12has debug => (
4387c692 13 traits => [qw(Getopt)],
14 cmd_aliases => 'd',
15 isa => Bool,
16 is => 'ro',
d3082fac 17 documentation => q{Force debug mode},
e46bf32c 18);
19
57dc50b0 20has host => (
4387c692 21 traits => [qw(Getopt)],
22 cmd_aliases => 'h',
23 isa => Str,
24 is => 'ro',
b1bfeea6 25 # N.B. undef (the default) means we bind on all interfaces on the host.
3bd410a9 26 documentation => 'Specify a hostname or IP on this host for the server to bind to',
41b55019 27);
90b481b1 28
57dc50b0 29has fork => (
4387c692 30 traits => [qw(Getopt)],
31 cmd_aliases => 'f',
32 isa => Bool,
33 is => 'ro',
34 default => 0,
53c6ec79 35 documentation => 'Fork the server to be able to serve multiple requests at once',
90b481b1 36);
37
bc6fa9fc 38has port => (
4387c692 39 traits => [qw(Getopt)],
40 cmd_aliases => 'p',
883c37ef 41 isa => PositiveInt,
4387c692 42 is => 'ro',
b89d8b98 43 default => sub {
56eb9db4 44 Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
b89d8b98 45 },
53c6ec79 46 documentation => 'Specify a different listening port (to the default port 3000)',
204c6935 47);
48
57dc50b0 49has pidfile => (
4387c692 50 traits => [qw(Getopt)],
51 cmd_aliases => 'pid',
52 isa => Str,
53 is => 'ro',
d3082fac 54 documentation => 'Specify a pidfile',
41b55019 55);
56
57dc50b0 57has keepalive => (
4387c692 58 traits => [qw(Getopt)],
59 cmd_aliases => 'k',
60 isa => Bool,
61 is => 'ro',
62 default => 0,
53c6ec79 63 documentation => 'Support keepalive',
bd31e11f 64);
65
57dc50b0 66has background => (
4387c692 67 traits => [qw(Getopt)],
68 cmd_aliases => 'bg',
69 isa => Bool,
70 is => 'ro',
71 default => 0,
d3082fac 72 documentation => 'Run in the background',
57dc50b0 73);
4b3881d4 74
8f01ed5f 75has restart => (
4387c692 76 traits => [qw(Getopt)],
77 cmd_aliases => 'r',
78 isa => Bool,
79 is => 'ro',
56eb9db4 80 default => sub {
81 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
82 },
53c6ec79 83 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
57dc50b0 84);
a7dea640 85
86has restart_directory => (
4387c692 87 traits => [qw(Getopt)],
1c517146 88 cmd_aliases => [ 'rdir', 'restartdirectory' ],
4387c692 89 isa => ArrayRef[Str],
90 is => 'ro',
d3082fac 91 documentation => 'Restarter directory to watch',
4387c692 92 predicate => '_has_restart_directory',
8f01ed5f 93);
94
57dc50b0 95has restart_delay => (
4387c692 96 traits => [qw(Getopt)],
97 cmd_aliases => 'rd',
98 isa => Int,
99 is => 'ro',
d3082fac 100 documentation => 'Set a restart delay',
4387c692 101 predicate => '_has_restart_delay',
70871584 102);
103
880d1f8b 104{
105 use Moose::Util::TypeConstraints;
106
107 my $tc = subtype as RegexpRef;
108 coerce $tc, from Str, via { qr/$_/ };
109
110 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
111
112 has restart_regex => (
4387c692 113 traits => [qw(Getopt)],
114 cmd_aliases => 'rr',
115 isa => $tc,
116 coerce => 1,
117 is => 'ro',
880d1f8b 118 documentation => 'Restart regex',
4387c692 119 predicate => '_has_restart_regex',
880d1f8b 120 );
121}
ee7aabd6 122
57dc50b0 123has follow_symlinks => (
4387c692 124 traits => [qw(Getopt)],
125 cmd_aliases => 'sym',
126 isa => Bool,
127 is => 'ro',
128 default => 0,
d3082fac 129 documentation => 'Follow symbolic links',
4387c692 130 predicate => '_has_follow_symlinks',
bbd42ac8 131);
a3ca4468 132
3453b929 133sub _restarter_args {
134 my $self = shift;
34be7d45 135
10255473 136 return (
34be7d45 137 argv => $self->ARGV,
138 start_sub => sub { $self->_run_application },
139 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
140 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
141 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
880d1f8b 142 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
a024e9ad 143 ),
144 (
8dde82d5 145 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
34be7d45 146 );
3453b929 147}
148
75fe0bb3 149has restarter_class => (
150 is => 'ro',
151 isa => Str,
152 lazy => 1,
153 default => sub {
154 my $self = shift;
155 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
156 }
157);
158
a3ca4468 159sub run {
3453b929 160 my $self = shift;
57dc50b0 161
53c6ec79 162 local $ENV{CATALYST_DEBUG} = 1
163 if $self->debug;
abee32cb 164
a7dea640 165 if ( $self->restart ) {
166 die "Cannot run in the background and also watch for changed files.\n"
167 if $self->background;
168
53c6ec79 169 # If we load this here, then in the case of a restarter, it does not
170 # need to be reloaded for each restart.
171 require Catalyst;
172
173 # If this isn't done, then the Catalyst::Devel tests for the restarter
174 # fail.
175 $| = 1 if $ENV{HARNESS_ACTIVE};
176
a024e9ad 177 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
a7dea640 178
75fe0bb3 179 my $subclass = $self->restarter_class->pick_subclass;
a7dea640 180
a7dea640 181 my $restarter = $subclass->new(
3453b929 182 $self->_restarter_args()
a7dea640 183 );
184
185 $restarter->run_and_watch;
186 }
187 else {
d3082fac 188 $self->_run_application;
a7dea640 189 }
190
191
57dc50b0 192}
193
36a53c3a 194sub _plack_loader_args {
195 my ($self) = shift;
196 return (
197 port => $self->port,
198 host => $self->host,
199 keepalive => $self->keepalive ? 100 : 1,
54ab9446 200 server_ready => sub {
201 my ($args) = @_;
202
203 my $name = $args->{server_software} || ref($args); # $args is $server
204 my $host = $args->{host} || 0;
205 my $proto = $args->{proto} || 'http';
206
207 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
208 },
36a53c3a 209 );
210}
211
d3082fac 212sub _application_args {
a7dea640 213 my ($self) = shift;
d3082fac 214 return (
bc6fa9fc 215 $self->port,
d3082fac 216 $self->host,
57dc50b0 217 {
30b70903 218 argv => $self->ARGV,
d3082fac 219 map { $_ => $self->$_ } qw/
220 fork
221 keepalive
222 background
223 pidfile
224 keepalive
225 follow_symlinks
226 /,
227 },
a3ca4468 228 );
a3ca4468 229}
5b923b0b 230
2e81e132 231__PACKAGE__->meta->make_immutable;
5b923b0b 232
0ba6e8aa 2331;
e46bf32c 234
235=head1 NAME
236
cbaaecc0 237Catalyst::Script::Server - Catalyst test server
e46bf32c 238
239=head1 SYNOPSIS
240
cbaaecc0 241 myapp_server.pl [options]
e46bf32c 242
243 Options:
4b3881d4 244 -d --debug force debug mode
245 -f --fork handle each request in a new process
e46bf32c 246 (defaults to false)
3dcfb4c8 247 --help display this help and exits
248 -h --host host (defaults to all)
4b3881d4 249 -p --port port (defaults to 3000)
250 -k --keepalive enable keep-alive connections
251 -r --restart restart when files get modified
252 (defaults to false)
87f5a448 253 --rd --restart_delay delay between file checks
e46bf32c 254 (ignored if you have Linux::Inotify2 installed)
87f5a448 255 --rr --restart_regex regex match files that trigger
e46bf32c 256 a restart when modified
257 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
87f5a448 258 --rdir --restart_directory the directory to search for
6ab9f4af 259 modified files, can be set multiple times
e46bf32c 260 (defaults to '[SCRIPT_DIR]/..')
4b3881d4 261 --sym --follow_symlinks follow symlinks in search directories
e46bf32c 262 (defaults to false. this is a no-op on Win32)
4b3881d4 263 --bg --background run the process in the background
264 --pid --pidfile specify filename for pid file
e46bf32c 265
266 See also:
267 perldoc Catalyst::Manual
268 perldoc Catalyst::Manual::Intro
269
270=head1 DESCRIPTION
271
cbaaecc0 272Run a Catalyst test server for this application.
e46bf32c 273
274=head1 AUTHORS
275
276Catalyst Contributors, see Catalyst.pm
277
278=head1 COPYRIGHT
279
280This library is free software. You can redistribute it and/or modify
281it under the same terms as Perl itself.
282
283=cut