1 package Catalyst::Script::Server;
4 use Class::Load qw(try_load_class load_class);
5 use namespace::clean -except => [ 'meta' ];
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)],
42 Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
44 documentation => 'Specify a different listening port (to the default port 3000)',
48 cmd_aliases => [ qw(usage ?) ],
51 use Moose::Util::TypeConstraints;
52 class_type 'MooseX::Daemonize::Pid::File';
53 subtype 'Catalyst::Script::Server::Types::Pidfile',
54 as 'MooseX::Daemonize::Pid::File';
56 coerce 'Catalyst::Script::Server::Types::Pidfile', from 'Str', via {
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;
60 MooseX::Daemonize::Pid::File->new( file => $_ );
62 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
63 'Catalyst::Script::Server::Types::Pidfile' => '=s',
66 traits => [qw(Getopt)],
68 isa => 'Catalyst::Script::Server::Types::Pidfile',
70 documentation => 'Specify a pidfile',
72 predicate => '_has_pidfile',
75 # Override MooseX::Daemonize
76 sub dont_close_all_files { 1 }
80 if ($self->background) {
81 # FIXME - This is evil. Should we just add MX::Daemonize to the deps?
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;
85 ($success, $error) = try_load_class("POSIX");
86 warn("$error\n"), exit 1 if not $success;
87 MooseX::Daemonize::Core->meta->apply($self);
88 POSIX::close($_) foreach (0..2);
93 traits => [qw(Getopt)],
98 documentation => 'Support keepalive',
102 traits => [qw(Getopt)],
107 documentation => 'Run in the background',
111 traits => [qw(Getopt)],
117 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
119 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
122 has restart_directory => (
123 traits => [qw(Getopt)],
124 cmd_aliases => [ 'rdir', 'restartdirectory' ],
125 isa => 'ArrayRef[Str]',
127 documentation => 'Restarter directory to watch',
128 predicate => '_has_restart_directory',
131 has restart_delay => (
132 traits => [qw(Getopt)],
136 documentation => 'Set a restart delay',
137 predicate => '_has_restart_delay',
141 use Moose::Util::TypeConstraints;
143 my $tc = subtype 'Catalyst::Script::Server::Types::RegexpRef', as 'RegexpRef';
144 coerce $tc, from 'Str', via { qr/$_/ };
146 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
148 has restart_regex => (
149 traits => [qw(Getopt)],
154 documentation => 'Restart regex',
155 predicate => '_has_restart_regex',
159 has follow_symlinks => (
160 traits => [qw(Getopt)],
161 cmd_aliases => 'sym',
165 documentation => 'Follow symbolic links',
166 predicate => '_has_follow_symlinks',
169 sub _plack_engine_name {
171 return $self->fork || $self->keepalive ? 'Starman' : 'Standalone';
174 sub _restarter_args {
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) : ()),
183 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
186 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
190 has restarter_class => (
196 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
203 local $ENV{CATALYST_DEBUG} = 1
206 if ( $self->restart ) {
207 die "Cannot run in the background and also watch for changed files.\n"
208 if $self->background;
209 die "Cannot write out a pid file and fork for the restarter.\n"
210 if $self->_has_pidfile;
212 # If we load this here, then in the case of a restarter, it does not
213 # need to be reloaded for each restart.
216 # If this isn't done, then the Catalyst::Devel tests for the restarter
218 $| = 1 if $ENV{HARNESS_ACTIVE};
220 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
222 my $subclass = $self->restarter_class->pick_subclass;
224 my $restarter = $subclass->new(
225 $self->_restarter_args()
228 $restarter->run_and_watch;
231 if ($self->background) {
234 return 1 unless $self->is_daemon;
236 load_class($self->application_name);
238 $self->daemon_detach;
241 $self->pidfile->write
242 if $self->_has_pidfile;
244 $self->_run_application;
250 sub _plack_loader_args {
255 keepalive => $self->keepalive ? 100 : 1,
256 server_ready => sub {
259 my $name = $args->{server_software} || ref($args); # $args is $server
260 my $host = $args->{host} || 0;
261 my $proto = $args->{proto} || 'http';
263 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
268 around _application_args => sub {
269 my ($orig, $self) = @_;
275 map { $_ => $self->$_ } qw/
289 __PACKAGE__->meta->make_immutable;
294 Catalyst::Script::Server - Catalyst test server
298 myapp_server.pl [options]
301 -d --debug force debug mode
302 -f --fork handle each request in a new process
304 --help display this help and exits
305 -h --host host (defaults to all)
306 -p --port port (defaults to 3000)
307 -k --keepalive enable keep-alive connections
308 -r --restart restart when files get modified
310 --rd --restart_delay delay between file checks
311 (ignored if you have Linux::Inotify2 installed)
312 --rr --restart_regex regex match files that trigger
313 a restart when modified
314 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
315 --rdir --restart_directory the directory to search for
316 modified files, can be set multiple times
317 (defaults to '[SCRIPT_DIR]/..')
318 --sym --follow_symlinks follow symlinks in search directories
319 (defaults to false. this is a no-op on Win32)
320 --bg --background run the process in the background
321 --pid --pidfile specify filename for pid file
324 perldoc Catalyst::Manual
325 perldoc Catalyst::Manual::Intro
329 Run a Catalyst test server for this application.
333 L<Catalyst::ScriptRunner>
337 Catalyst Contributors, see Catalyst.pm
341 This library is free software. You can redistribute it and/or modify
342 it under the same terms as Perl itself.