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