1 package Catalyst::Script::Server;
3 use MooseX::Types::Common::Numeric qw/PositiveInt/;
4 use MooseX::Types::Moose qw/ArrayRef Str Bool Int RegexpRef/;
6 use namespace::autoclean;
8 with 'Catalyst::ScriptRole';
11 traits => [qw(Getopt)],
15 documentation => q{Force debug mode},
19 traits => [qw(Getopt)],
23 # N.B. undef (the default) means we bind on all interfaces on the host.
24 documentation => 'Specify a hostname or IP on this host for the server to bind to',
28 traits => [qw(Getopt)],
33 documentation => 'Fork the server to be able to serve multiple requests at once',
37 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)',
47 use Moose::Util::TypeConstraints;
48 class_type 'MooseX::Daemonize::Pid::File';
49 subtype 'MyStr', as Str, where { 1 }; # FIXME - Fuck ugly!
50 coerce 'MooseX::Daemonize::Pid::File', from 'MyStr', via {
51 Class::MOP::load_class("MooseX::Daemonize::Pid::File");
52 MooseX::Daemonize::Pid::File->new( file => $_ );
54 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
55 'MooseX::Daemonize::Pid::File' => '=s',
58 traits => [qw(Getopt)],
60 isa => 'MooseX::Daemonize::Pid::File',
62 documentation => 'Specify a pidfile',
64 predicate => '_has_pidfile',
70 if ($self->background) {
71 # FIXME - This is evil. Should we just add MX::Daemonize to the deps?
72 Class::MOP::load_class('MooseX::Daemonize::Core');
73 MooseX::Daemonize::Core->meta->apply($self);
78 traits => [qw(Getopt)],
83 documentation => 'Support keepalive',
87 traits => [qw(Getopt)],
92 documentation => 'Run in the background',
96 traits => [qw(Getopt)],
101 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
103 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
106 has restart_directory => (
107 traits => [qw(Getopt)],
108 cmd_aliases => [ 'rdir', 'restartdirectory' ],
109 isa => ArrayRef[Str],
111 documentation => 'Restarter directory to watch',
112 predicate => '_has_restart_directory',
115 has restart_delay => (
116 traits => [qw(Getopt)],
120 documentation => 'Set a restart delay',
121 predicate => '_has_restart_delay',
125 use Moose::Util::TypeConstraints;
127 my $tc = subtype as RegexpRef;
128 coerce $tc, from Str, via { qr/$_/ };
130 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
132 has restart_regex => (
133 traits => [qw(Getopt)],
138 documentation => 'Restart regex',
139 predicate => '_has_restart_regex',
143 has follow_symlinks => (
144 traits => [qw(Getopt)],
145 cmd_aliases => 'sym',
149 documentation => 'Follow symbolic links',
150 predicate => '_has_follow_symlinks',
153 sub _plack_engine_name {
155 return $self->fork ? 'Starman' : $self->keepalive ? 'Starman' : 'Standalone';
158 sub _restarter_args {
163 start_sub => sub { $self->_run_application },
164 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
165 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
166 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
167 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
170 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
174 has restarter_class => (
180 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
187 local $ENV{CATALYST_DEBUG} = 1
190 if ( $self->restart ) {
191 die "Cannot run in the background and also watch for changed files.\n"
192 if $self->background;
193 die "Cannot write out a pid file and fork for the restarter.\n"
194 if $self->_has_pidfile;
196 # If we load this here, then in the case of a restarter, it does not
197 # need to be reloaded for each restart.
200 # If this isn't done, then the Catalyst::Devel tests for the restarter
202 $| = 1 if $ENV{HARNESS_ACTIVE};
204 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
206 my $subclass = $self->restarter_class->pick_subclass;
208 my $restarter = $subclass->new(
209 $self->_restarter_args()
212 $restarter->run_and_watch;
215 if ($self->background) {
218 return 1 unless $self->is_daemon;
220 Class::MOP::load_class($self->application_name);
222 $self->daemon_detach;
225 $self->pidfile->write
226 if $self->_has_pidfile;
228 $self->_run_application;
234 sub _plack_loader_args {
239 keepalive => $self->keepalive ? 100 : 1,
240 server_ready => sub {
243 my $name = $args->{server_software} || ref($args); # $args is $server
244 my $host = $args->{host} || 0;
245 my $proto = $args->{proto} || 'http';
247 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
252 sub _application_args {
259 map { $_ => $self->$_ } qw/
271 __PACKAGE__->meta->make_immutable;
277 Catalyst::Script::Server - Catalyst test server
281 myapp_server.pl [options]
284 -d --debug force debug mode
285 -f --fork handle each request in a new process
287 --help display this help and exits
288 -h --host host (defaults to all)
289 -p --port port (defaults to 3000)
290 -k --keepalive enable keep-alive connections
291 -r --restart restart when files get modified
293 --rd --restart_delay delay between file checks
294 (ignored if you have Linux::Inotify2 installed)
295 --rr --restart_regex regex match files that trigger
296 a restart when modified
297 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
298 --rdir --restart_directory the directory to search for
299 modified files, can be set multiple times
300 (defaults to '[SCRIPT_DIR]/..')
301 --sym --follow_symlinks follow symlinks in search directories
302 (defaults to false. this is a no-op on Win32)
303 --bg --background run the process in the background
304 --pid --pidfile specify filename for pid file
307 perldoc Catalyst::Manual
308 perldoc Catalyst::Manual::Intro
312 Run a Catalyst test server for this application.
316 Catalyst Contributors, see Catalyst.pm
320 This library is free software. You can redistribute it and/or modify
321 it under the same terms as Perl itself.