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;
74 traits => [qw(Getopt)],
79 documentation => 'Support keepalive',
83 traits => [qw(Getopt)],
88 documentation => 'Run in the background',
92 traits => [qw(Getopt)],
97 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
99 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
102 has restart_directory => (
103 traits => [qw(Getopt)],
104 cmd_aliases => [ 'rdir', 'restartdirectory' ],
105 isa => ArrayRef[Str],
107 documentation => 'Restarter directory to watch',
108 predicate => '_has_restart_directory',
111 has restart_delay => (
112 traits => [qw(Getopt)],
116 documentation => 'Set a restart delay',
117 predicate => '_has_restart_delay',
121 use Moose::Util::TypeConstraints;
123 my $tc = subtype as RegexpRef;
124 coerce $tc, from Str, via { qr/$_/ };
126 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
128 has restart_regex => (
129 traits => [qw(Getopt)],
134 documentation => 'Restart regex',
135 predicate => '_has_restart_regex',
139 has follow_symlinks => (
140 traits => [qw(Getopt)],
141 cmd_aliases => 'sym',
145 documentation => 'Follow symbolic links',
146 predicate => '_has_follow_symlinks',
149 sub _plack_engine_name {
151 return $self->fork ? 'Starman' : $self->keepalive ? 'Starman' : 'Standalone';
154 sub _restarter_args {
159 start_sub => sub { $self->_run_application },
160 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
161 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
162 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
163 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
166 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
170 has restarter_class => (
176 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
183 local $ENV{CATALYST_DEBUG} = 1
186 if ( $self->restart ) {
187 die "Cannot run in the background and also watch for changed files.\n"
188 if $self->background;
190 # If we load this here, then in the case of a restarter, it does not
191 # need to be reloaded for each restart.
194 # If this isn't done, then the Catalyst::Devel tests for the restarter
196 $| = 1 if $ENV{HARNESS_ACTIVE};
198 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
200 my $subclass = $self->restarter_class->pick_subclass;
202 my $restarter = $subclass->new(
203 $self->_restarter_args()
206 $restarter->run_and_watch;
209 $self->_run_application;
215 sub _plack_loader_args {
220 keepalive => $self->keepalive ? 100 : 1,
221 server_ready => sub {
224 my $name = $args->{server_software} || ref($args); # $args is $server
225 my $host = $args->{host} || 0;
226 my $proto = $args->{proto} || 'http';
228 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
233 sub _application_args {
240 map { $_ => $self->$_ } qw/
252 __PACKAGE__->meta->make_immutable;
258 Catalyst::Script::Server - Catalyst test server
262 myapp_server.pl [options]
265 -d --debug force debug mode
266 -f --fork handle each request in a new process
268 --help display this help and exits
269 -h --host host (defaults to all)
270 -p --port port (defaults to 3000)
271 -k --keepalive enable keep-alive connections
272 -r --restart restart when files get modified
274 --rd --restart_delay delay between file checks
275 (ignored if you have Linux::Inotify2 installed)
276 --rr --restart_regex regex match files that trigger
277 a restart when modified
278 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
279 --rdir --restart_directory the directory to search for
280 modified files, can be set multiple times
281 (defaults to '[SCRIPT_DIR]/..')
282 --sym --follow_symlinks follow symlinks in search directories
283 (defaults to false. this is a no-op on Win32)
284 --bg --background run the process in the background
285 --pid --pidfile specify filename for pid file
288 perldoc Catalyst::Manual
289 perldoc Catalyst::Manual::Intro
293 Run a Catalyst test server for this application.
297 Catalyst Contributors, see Catalyst.pm
301 This library is free software. You can redistribute it and/or modify
302 it under the same terms as Perl itself.