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->_has_pidfile;
71 if ($self->background) {
72 # FIXME - This is evil. Should we just add MX::Daemonize to the deps?
73 Class::MOP::load_class('MooseX::Daemonize::Core');
74 MooseX::Daemonize::Core->meta->apply($self);
79 traits => [qw(Getopt)],
84 documentation => 'Support keepalive',
88 traits => [qw(Getopt)],
93 documentation => 'Run in the background',
97 traits => [qw(Getopt)],
102 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
104 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
107 has restart_directory => (
108 traits => [qw(Getopt)],
109 cmd_aliases => [ 'rdir', 'restartdirectory' ],
110 isa => ArrayRef[Str],
112 documentation => 'Restarter directory to watch',
113 predicate => '_has_restart_directory',
116 has restart_delay => (
117 traits => [qw(Getopt)],
121 documentation => 'Set a restart delay',
122 predicate => '_has_restart_delay',
126 use Moose::Util::TypeConstraints;
128 my $tc = subtype as RegexpRef;
129 coerce $tc, from Str, via { qr/$_/ };
131 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
133 has restart_regex => (
134 traits => [qw(Getopt)],
139 documentation => 'Restart regex',
140 predicate => '_has_restart_regex',
144 has follow_symlinks => (
145 traits => [qw(Getopt)],
146 cmd_aliases => 'sym',
150 documentation => 'Follow symbolic links',
151 predicate => '_has_follow_symlinks',
154 sub _plack_engine_name {
156 return $self->fork ? 'Starman' : $self->keepalive ? 'Starman' : 'Standalone';
159 sub _restarter_args {
164 start_sub => sub { $self->_run_application },
165 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
166 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
167 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
168 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
171 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
175 has restarter_class => (
181 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
188 local $ENV{CATALYST_DEBUG} = 1
191 if ( $self->restart ) {
192 die "Cannot run in the background and also watch for changed files.\n"
193 if $self->background;
195 # If we load this here, then in the case of a restarter, it does not
196 # need to be reloaded for each restart.
199 # If this isn't done, then the Catalyst::Devel tests for the restarter
201 $| = 1 if $ENV{HARNESS_ACTIVE};
203 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
205 my $subclass = $self->restarter_class->pick_subclass;
207 my $restarter = $subclass->new(
208 $self->_restarter_args()
211 $restarter->run_and_watch;
214 if ($self->background) {
217 return 1 unless $self->is_daemon;
219 Class::MOP::load_class($self->application_name);
221 $self->daemon_detach;
224 $self->_run_application;
230 sub _plack_loader_args {
235 keepalive => $self->keepalive ? 100 : 1,
236 server_ready => sub {
239 my $name = $args->{server_software} || ref($args); # $args is $server
240 my $host = $args->{host} || 0;
241 my $proto = $args->{proto} || 'http';
243 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
248 sub _application_args {
255 map { $_ => $self->$_ } qw/
267 __PACKAGE__->meta->make_immutable;
273 Catalyst::Script::Server - Catalyst test server
277 myapp_server.pl [options]
280 -d --debug force debug mode
281 -f --fork handle each request in a new process
283 --help display this help and exits
284 -h --host host (defaults to all)
285 -p --port port (defaults to 3000)
286 -k --keepalive enable keep-alive connections
287 -r --restart restart when files get modified
289 --rd --restart_delay delay between file checks
290 (ignored if you have Linux::Inotify2 installed)
291 --rr --restart_regex regex match files that trigger
292 a restart when modified
293 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
294 --rdir --restart_directory the directory to search for
295 modified files, can be set multiple times
296 (defaults to '[SCRIPT_DIR]/..')
297 --sym --follow_symlinks follow symlinks in search directories
298 (defaults to false. this is a no-op on Win32)
299 --bg --background run the process in the background
300 --pid --pidfile specify filename for pid file
303 perldoc Catalyst::Manual
304 perldoc Catalyst::Manual::Intro
308 Run a Catalyst test server for this application.
312 Catalyst Contributors, see Catalyst.pm
316 This library is free software. You can redistribute it and/or modify
317 it under the same terms as Perl itself.