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',
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') }
81 warn("MooseX::Daemonize is needed for the --background option\n");
84 MooseX::Daemonize::Core->meta->apply($self);
89 traits => [qw(Getopt)],
94 documentation => 'Support keepalive',
98 traits => [qw(Getopt)],
103 documentation => 'Run in the background',
107 traits => [qw(Getopt)],
112 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
114 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
117 has restart_directory => (
118 traits => [qw(Getopt)],
119 cmd_aliases => [ 'rdir', 'restartdirectory' ],
120 isa => ArrayRef[Str],
122 documentation => 'Restarter directory to watch',
123 predicate => '_has_restart_directory',
126 has restart_delay => (
127 traits => [qw(Getopt)],
131 documentation => 'Set a restart delay',
132 predicate => '_has_restart_delay',
136 use Moose::Util::TypeConstraints;
138 my $tc = subtype 'Catalyst::Script::Server::Types::RegexpRef', as RegexpRef;
139 coerce $tc, from Str, via { qr/$_/ };
141 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
143 has restart_regex => (
144 traits => [qw(Getopt)],
149 documentation => 'Restart regex',
150 predicate => '_has_restart_regex',
154 has follow_symlinks => (
155 traits => [qw(Getopt)],
156 cmd_aliases => 'sym',
160 documentation => 'Follow symbolic links',
161 predicate => '_has_follow_symlinks',
164 sub _plack_engine_name {
166 return $self->fork ? 'Starman' : $self->keepalive ? 'Starman' : 'Standalone';
169 sub _restarter_args {
174 start_sub => sub { $self->_run_application },
175 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
176 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
177 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
178 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
181 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
185 has restarter_class => (
191 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
198 local $ENV{CATALYST_DEBUG} = 1
201 if ( $self->restart ) {
202 die "Cannot run in the background and also watch for changed files.\n"
203 if $self->background;
204 die "Cannot write out a pid file and fork for the restarter.\n"
205 if $self->_has_pidfile;
207 # If we load this here, then in the case of a restarter, it does not
208 # need to be reloaded for each restart.
211 # If this isn't done, then the Catalyst::Devel tests for the restarter
213 $| = 1 if $ENV{HARNESS_ACTIVE};
215 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
217 my $subclass = $self->restarter_class->pick_subclass;
219 my $restarter = $subclass->new(
220 $self->_restarter_args()
223 $restarter->run_and_watch;
226 if ($self->background) {
229 return 1 unless $self->is_daemon;
231 Class::MOP::load_class($self->application_name);
233 $self->daemon_detach;
236 $self->pidfile->write
237 if $self->_has_pidfile;
239 $self->_run_application;
245 sub _plack_loader_args {
250 keepalive => $self->keepalive ? 100 : 1,
251 server_ready => sub {
254 my $name = $args->{server_software} || ref($args); # $args is $server
255 my $host = $args->{host} || 0;
256 my $proto = $args->{proto} || 'http';
258 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
263 sub _application_args {
270 map { $_ => $self->$_ } qw/
282 __PACKAGE__->meta->make_immutable;
288 Catalyst::Script::Server - Catalyst test server
292 myapp_server.pl [options]
295 -d --debug force debug mode
296 -f --fork handle each request in a new process
298 --help display this help and exits
299 -h --host host (defaults to all)
300 -p --port port (defaults to 3000)
301 -k --keepalive enable keep-alive connections
302 -r --restart restart when files get modified
304 --rd --restart_delay delay between file checks
305 (ignored if you have Linux::Inotify2 installed)
306 --rr --restart_regex regex match files that trigger
307 a restart when modified
308 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
309 --rdir --restart_directory the directory to search for
310 modified files, can be set multiple times
311 (defaults to '[SCRIPT_DIR]/..')
312 --sym --follow_symlinks follow symlinks in search directories
313 (defaults to false. this is a no-op on Win32)
314 --bg --background run the process in the background
315 --pid --pidfile specify filename for pid file
318 perldoc Catalyst::Manual
319 perldoc Catalyst::Manual::Intro
323 Run a Catalyst test server for this application.
327 Catalyst Contributors, see Catalyst.pm
331 This library is free software. You can redistribute it and/or modify
332 it under the same terms as Perl itself.