1 package Catalyst::Script::Server;
5 use namespace::autoclean;
7 with 'Catalyst::ScriptRole';
10 traits => [qw(Getopt)],
14 documentation => q{Force debug mode},
18 traits => [qw(Getopt)],
22 # N.B. undef (the default) means we bind on all interfaces on the host.
23 documentation => 'Specify a hostname or IP on this host for the server to bind to',
27 traits => [qw(Getopt)],
32 documentation => 'Fork the server to be able to serve multiple requests at once',
36 traits => [qw(Getopt)],
41 Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
43 documentation => 'Specify a different listening port (to the default port 3000)',
46 use Moose::Util::TypeConstraints;
47 class_type 'MooseX::Daemonize::Pid::File';
48 subtype 'Catalyst::Script::Server::Types::Pidfile',
49 as 'MooseX::Daemonize::Pid::File';
51 coerce 'Catalyst::Script::Server::Types::Pidfile', from 'Str', via {
52 try { Class::MOP::load_class("MooseX::Daemonize::Pid::File") }
54 warn("Could not load MooseX::Daemonize::Pid::File, needed for --pid option\n");
57 MooseX::Daemonize::Pid::File->new( file => $_ );
59 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
60 'Catalyst::Script::Server::Types::Pidfile' => '=s',
63 traits => [qw(Getopt)],
65 isa => 'Catalyst::Script::Server::Types::Pidfile',
67 documentation => 'Specify a pidfile',
69 predicate => '_has_pidfile',
72 # Override MooseX::Daemonize
73 sub dont_close_all_files { 1 }
77 if ($self->background) {
78 # FIXME - This is evil. Should we just add MX::Daemonize to the deps?
79 try { Class::MOP::load_class('MooseX::Daemonize::Core'); Class::MOP::load_class('POSIX') }
81 warn("MooseX::Daemonize is needed for the --background option\n");
84 MooseX::Daemonize::Core->meta->apply($self);
85 POSIX::close($_) foreach (0..2);
90 traits => [qw(Getopt)],
95 documentation => 'Support keepalive',
99 traits => [qw(Getopt)],
104 documentation => 'Run in the background',
108 traits => [qw(Getopt)],
113 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
115 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
118 has restart_directory => (
119 traits => [qw(Getopt)],
120 cmd_aliases => [ 'rdir', 'restartdirectory' ],
121 isa => 'ArrayRef[Str]',
123 documentation => 'Restarter directory to watch',
124 predicate => '_has_restart_directory',
127 has restart_delay => (
128 traits => [qw(Getopt)],
132 documentation => 'Set a restart delay',
133 predicate => '_has_restart_delay',
137 use Moose::Util::TypeConstraints;
139 my $tc = subtype 'Catalyst::Script::Server::Types::RegexpRef', as 'RegexpRef';
140 coerce $tc, from 'Str', via { qr/$_/ };
142 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
144 has restart_regex => (
145 traits => [qw(Getopt)],
150 documentation => 'Restart regex',
151 predicate => '_has_restart_regex',
155 has follow_symlinks => (
156 traits => [qw(Getopt)],
157 cmd_aliases => 'sym',
161 documentation => 'Follow symbolic links',
162 predicate => '_has_follow_symlinks',
165 sub _plack_engine_name {
167 return $self->fork || $self->keepalive ? 'Starman' : 'Standalone';
170 sub _restarter_args {
175 start_sub => sub { $self->_run_application },
176 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
177 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
178 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
179 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
182 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
186 has restarter_class => (
192 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
199 local $ENV{CATALYST_DEBUG} = 1
202 if ( $self->restart ) {
203 die "Cannot run in the background and also watch for changed files.\n"
204 if $self->background;
205 die "Cannot write out a pid file and fork for the restarter.\n"
206 if $self->_has_pidfile;
208 # If we load this here, then in the case of a restarter, it does not
209 # need to be reloaded for each restart.
212 # If this isn't done, then the Catalyst::Devel tests for the restarter
214 $| = 1 if $ENV{HARNESS_ACTIVE};
216 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
218 my $subclass = $self->restarter_class->pick_subclass;
220 my $restarter = $subclass->new(
221 $self->_restarter_args()
224 $restarter->run_and_watch;
227 if ($self->background) {
230 return 1 unless $self->is_daemon;
232 Class::MOP::load_class($self->application_name);
234 $self->daemon_detach;
237 $self->pidfile->write
238 if $self->_has_pidfile;
240 $self->_run_application;
246 sub _plack_loader_args {
251 keepalive => $self->keepalive ? 100 : 1,
252 server_ready => sub {
255 my $name = $args->{server_software} || ref($args); # $args is $server
256 my $host = $args->{host} || 0;
257 my $proto = $args->{proto} || 'http';
259 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
264 around _application_args => sub {
265 my ($orig, $self) = @_;
271 map { $_ => $self->$_ } qw/
285 __PACKAGE__->meta->make_immutable;
290 Catalyst::Script::Server - Catalyst test server
294 myapp_server.pl [options]
297 -d --debug force debug mode
298 -f --fork handle each request in a new process
300 --help display this help and exits
301 -h --host host (defaults to all)
302 -p --port port (defaults to 3000)
303 -k --keepalive enable keep-alive connections
304 -r --restart restart when files get modified
306 --rd --restart_delay delay between file checks
307 (ignored if you have Linux::Inotify2 installed)
308 --rr --restart_regex regex match files that trigger
309 a restart when modified
310 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
311 --rdir --restart_directory the directory to search for
312 modified files, can be set multiple times
313 (defaults to '[SCRIPT_DIR]/..')
314 --sym --follow_symlinks follow symlinks in search directories
315 (defaults to false. this is a no-op on Win32)
316 --bg --background run the process in the background
317 --pid --pidfile specify filename for pid file
320 perldoc Catalyst::Manual
321 perldoc Catalyst::Manual::Intro
325 Run a Catalyst test server for this application.
329 L<Catalyst::ScriptRunner>
333 Catalyst Contributors, see Catalyst.pm
337 This library is free software. You can redistribute it and/or modify
338 it under the same terms as Perl itself.