1 package Catalyst::Script::Server;
4 use Class::Load qw(try_load_class load_class);
5 use namespace::autoclean;
7 with 'Catalyst::ScriptRole';
10 traits => [qw(Getopt)],
14 documentation => q{Force debug mode},
18 traits => [qw(Getopt)],
22 documentation => q{Force trace mode},
26 traits => [qw(Getopt)],
30 # N.B. undef (the default) means we bind on all interfaces on the host.
31 documentation => 'Specify a hostname or IP on this host for the server to bind to',
35 traits => [qw(Getopt)],
40 documentation => 'Fork the server to be able to serve multiple requests at once',
44 traits => [qw(Getopt)],
49 Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
51 documentation => 'Specify a different listening port (to the default port 3000)',
54 use Moose::Util::TypeConstraints;
55 class_type 'MooseX::Daemonize::Pid::File';
56 subtype 'Catalyst::Script::Server::Types::Pidfile',
57 as 'MooseX::Daemonize::Pid::File';
59 coerce 'Catalyst::Script::Server::Types::Pidfile', from 'Str', via {
60 my ($success, $error) = try_load_class("MooseX::Daemonize::Pid::File");
61 warn("Could not load MooseX::Daemonize::Pid::File, needed for --pid option: $error\n"),
62 exit 1 if not $success;
63 MooseX::Daemonize::Pid::File->new( file => $_ );
65 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
66 'Catalyst::Script::Server::Types::Pidfile' => '=s',
69 traits => [qw(Getopt)],
71 isa => 'Catalyst::Script::Server::Types::Pidfile',
73 documentation => 'Specify a pidfile',
75 predicate => '_has_pidfile',
78 # Override MooseX::Daemonize
79 sub dont_close_all_files { 1 }
83 if ($self->background) {
84 # FIXME - This is evil. Should we just add MX::Daemonize to the deps?
85 my ($success, $error) = try_load_class("MooseX::Daemonize::Core");
86 warn("MooseX::Daemonize is needed for the --background option: $error\n"),
87 exit 1 if not $success;
88 ($success, $error) = try_load_class("POSIX");
89 warn("$error\n"), exit 1 if not $success;
90 MooseX::Daemonize::Core->meta->apply($self);
91 POSIX::close($_) foreach (0..2);
96 traits => [qw(Getopt)],
101 documentation => 'Support keepalive',
105 traits => [qw(Getopt)],
110 documentation => 'Run in the background',
114 traits => [qw(Getopt)],
119 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
121 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
124 has restart_directory => (
125 traits => [qw(Getopt)],
126 cmd_aliases => [ 'rdir', 'restartdirectory' ],
127 isa => 'ArrayRef[Str]',
129 documentation => 'Restarter directory to watch',
130 predicate => '_has_restart_directory',
133 has restart_delay => (
134 traits => [qw(Getopt)],
138 documentation => 'Set a restart delay',
139 predicate => '_has_restart_delay',
143 use Moose::Util::TypeConstraints;
145 my $tc = subtype 'Catalyst::Script::Server::Types::RegexpRef', as 'RegexpRef';
146 coerce $tc, from 'Str', via { qr/$_/ };
148 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
150 has restart_regex => (
151 traits => [qw(Getopt)],
156 documentation => 'Restart regex',
157 predicate => '_has_restart_regex',
161 has follow_symlinks => (
162 traits => [qw(Getopt)],
163 cmd_aliases => 'sym',
167 documentation => 'Follow symbolic links',
168 predicate => '_has_follow_symlinks',
171 sub _plack_engine_name {
173 return $self->fork || $self->keepalive ? 'Starman' : 'Standalone';
176 sub _restarter_args {
181 start_sub => sub { $self->_run_application },
182 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
183 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
184 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
185 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
188 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
192 has restarter_class => (
198 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
205 local $ENV{CATALYST_DEBUG} = 1
208 local $ENV{CATALYST_TRACE} = $self->trace
211 if ( $self->restart ) {
212 die "Cannot run in the background and also watch for changed files.\n"
213 if $self->background;
214 die "Cannot write out a pid file and fork for the restarter.\n"
215 if $self->_has_pidfile;
217 # If we load this here, then in the case of a restarter, it does not
218 # need to be reloaded for each restart.
221 # If this isn't done, then the Catalyst::Devel tests for the restarter
223 $| = 1 if $ENV{HARNESS_ACTIVE};
225 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
227 my $subclass = $self->restarter_class->pick_subclass;
229 my $restarter = $subclass->new(
230 $self->_restarter_args()
233 $restarter->run_and_watch;
236 if ($self->background) {
239 return 1 unless $self->is_daemon;
241 load_class($self->application_name);
243 $self->daemon_detach;
246 $self->pidfile->write
247 if $self->_has_pidfile;
249 $self->_run_application;
255 sub _plack_loader_args {
260 keepalive => $self->keepalive ? 100 : 1,
261 server_ready => sub {
264 my $name = $args->{server_software} || ref($args); # $args is $server
265 my $host = $args->{host} || 0;
266 my $proto = $args->{proto} || 'http';
268 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
273 around _application_args => sub {
274 my ($orig, $self) = @_;
280 map { $_ => $self->$_ } qw/
294 __PACKAGE__->meta->make_immutable;
299 Catalyst::Script::Server - Catalyst test server
303 myapp_server.pl [options]
306 -d --debug force debug mode
307 -f --fork handle each request in a new process
309 --help display this help and exits
310 -h --host host (defaults to all)
311 -p --port port (defaults to 3000)
312 -k --keepalive enable keep-alive connections
313 -r --restart restart when files get modified
315 --rd --restart_delay delay between file checks
316 (ignored if you have Linux::Inotify2 installed)
317 --rr --restart_regex regex match files that trigger
318 a restart when modified
319 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
320 --rdir --restart_directory the directory to search for
321 modified files, can be set multiple times
322 (defaults to '[SCRIPT_DIR]/..')
323 --sym --follow_symlinks follow symlinks in search directories
324 (defaults to false. this is a no-op on Win32)
325 --bg --background run the process in the background
326 --pid --pidfile specify filename for pid file
329 perldoc Catalyst::Manual
330 perldoc Catalyst::Manual::Intro
334 Run a Catalyst test server for this application.
338 L<Catalyst::ScriptRunner>
342 Catalyst Contributors, see Catalyst.pm
346 This library is free software. You can redistribute it and/or modify
347 it under the same terms as Perl itself.