1 package Catalyst::Script::Server;
3 use MooseX::Types::Common::Numeric qw/PositiveInt/;
4 use MooseX::Types::Moose qw/ArrayRef Str Bool Int RegexpRef/;
7 use namespace::autoclean;
9 with 'Catalyst::ScriptRole';
12 traits => [qw(Getopt)],
16 documentation => q{Force debug mode},
20 traits => [qw(Getopt)],
24 # N.B. undef (the default) means we bind on all interfaces on the host.
25 documentation => 'Specify a hostname or IP on this host for the server to bind to',
29 traits => [qw(Getopt)],
34 documentation => 'Fork the server to be able to serve multiple requests at once',
38 traits => [qw(Getopt)],
43 Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
45 documentation => 'Specify a different listening port (to the default port 3000)',
48 use Moose::Util::TypeConstraints;
49 class_type 'MooseX::Daemonize::Pid::File';
50 subtype 'Catalyst::Script::Server::Types::Pidfile',
51 as 'MooseX::Daemonize::Pid::File';
53 coerce 'Catalyst::Script::Server::Types::Pidfile', from Str, via {
54 try { Class::MOP::load_class("MooseX::Daemonize::Pid::File") }
56 warn("Could not load MooseX::Daemonize::Pid::File, needed for --pid option\n");
59 MooseX::Daemonize::Pid::File->new( file => $_ );
61 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
62 'Catalyst::Script::Server::Types::Pidfile' => '=s',
65 traits => [qw(Getopt)],
67 isa => 'Catalyst::Script::Server::Types::Pidfile',
69 documentation => 'Specify a pidfile',
71 predicate => '_has_pidfile',
74 # Override MooseX::Daemonize
75 sub dont_close_all_files { 1 }
79 if ($self->background) {
80 # FIXME - This is evil. Should we just add MX::Daemonize to the deps?
81 try { Class::MOP::load_class('MooseX::Daemonize::Core'); Class::MOP::load_class('POSIX') }
83 warn("MooseX::Daemonize is needed for the --background option\n");
86 MooseX::Daemonize::Core->meta->apply($self);
87 POSIX::close($_) foreach (0..2);
92 traits => [qw(Getopt)],
97 documentation => 'Support keepalive',
101 traits => [qw(Getopt)],
106 documentation => 'Run in the background',
110 traits => [qw(Getopt)],
115 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
117 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
120 has restart_directory => (
121 traits => [qw(Getopt)],
122 cmd_aliases => [ 'rdir', 'restartdirectory' ],
123 isa => ArrayRef[Str],
125 documentation => 'Restarter directory to watch',
126 predicate => '_has_restart_directory',
129 has restart_delay => (
130 traits => [qw(Getopt)],
134 documentation => 'Set a restart delay',
135 predicate => '_has_restart_delay',
139 use Moose::Util::TypeConstraints;
141 my $tc = subtype 'Catalyst::Script::Server::Types::RegexpRef', as RegexpRef;
142 coerce $tc, from Str, via { qr/$_/ };
144 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
146 has restart_regex => (
147 traits => [qw(Getopt)],
152 documentation => 'Restart regex',
153 predicate => '_has_restart_regex',
157 has follow_symlinks => (
158 traits => [qw(Getopt)],
159 cmd_aliases => 'sym',
163 documentation => 'Follow symbolic links',
164 predicate => '_has_follow_symlinks',
167 sub _plack_engine_name {
169 return $self->fork || $self->keepalive ? 'Starman' : 'Standalone';
172 sub _restarter_args {
177 start_sub => sub { $self->_run_application },
178 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
179 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
180 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
181 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
184 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
188 has restarter_class => (
194 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
201 local $ENV{CATALYST_DEBUG} = 1
204 if ( $self->restart ) {
205 die "Cannot run in the background and also watch for changed files.\n"
206 if $self->background;
207 die "Cannot write out a pid file and fork for the restarter.\n"
208 if $self->_has_pidfile;
210 # If we load this here, then in the case of a restarter, it does not
211 # need to be reloaded for each restart.
214 # If this isn't done, then the Catalyst::Devel tests for the restarter
216 $| = 1 if $ENV{HARNESS_ACTIVE};
218 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
220 my $subclass = $self->restarter_class->pick_subclass;
222 my $restarter = $subclass->new(
223 $self->_restarter_args()
226 $restarter->run_and_watch;
229 if ($self->background) {
232 return 1 unless $self->is_daemon;
234 Class::MOP::load_class($self->application_name);
236 $self->daemon_detach;
239 $self->pidfile->write
240 if $self->_has_pidfile;
242 $self->_run_application;
248 sub _plack_loader_args {
253 keepalive => $self->keepalive ? 100 : 1,
254 server_ready => sub {
257 my $name = $args->{server_software} || ref($args); # $args is $server
258 my $host = $args->{host} || 0;
259 my $proto = $args->{proto} || 'http';
261 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
266 around _application_args => sub {
267 my ($orig, $self) = @_;
273 map { $_ => $self->$_ } qw/
287 __PACKAGE__->meta->make_immutable;
292 Catalyst::Script::Server - Catalyst test server
296 myapp_server.pl [options]
299 -d --debug force debug mode
300 -f --fork handle each request in a new process
302 --help display this help and exits
303 -h --host host (defaults to all)
304 -p --port port (defaults to 3000)
305 -k --keepalive enable keep-alive connections
306 -r --restart restart when files get modified
308 --rd --restart_delay delay between file checks
309 (ignored if you have Linux::Inotify2 installed)
310 --rr --restart_regex regex match files that trigger
311 a restart when modified
312 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
313 --rdir --restart_directory the directory to search for
314 modified files, can be set multiple times
315 (defaults to '[SCRIPT_DIR]/..')
316 --sym --follow_symlinks follow symlinks in search directories
317 (defaults to false. this is a no-op on Win32)
318 --bg --background run the process in the background
319 --pid --pidfile specify filename for pid file
322 perldoc Catalyst::Manual
323 perldoc Catalyst::Manual::Intro
327 Run a Catalyst test server for this application.
331 L<Catalyst::ScriptRunner>
335 Catalyst Contributors, see Catalyst.pm
339 This library is free software. You can redistribute it and/or modify
340 it under the same terms as Perl itself.