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 sub _plack_engine_name { 'Standalone' }
10 with 'Catalyst::ScriptRole';
13 traits => [qw(Getopt)],
17 documentation => q{Force debug mode},
21 traits => [qw(Getopt)],
25 # N.B. undef (the default) means we bind on all interfaces on the host.
26 documentation => 'Specify a hostname or IP on this host for the server to bind to',
30 traits => [qw(Getopt)],
35 documentation => 'Fork the server to be able to serve multiple requests at once',
39 traits => [qw(Getopt)],
44 Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
46 documentation => 'Specify a different listening port (to the default port 3000)',
50 traits => [qw(Getopt)],
54 documentation => 'Specify a pidfile',
58 traits => [qw(Getopt)],
63 documentation => 'Support keepalive',
67 traits => [qw(Getopt)],
72 documentation => 'Run in the background',
76 traits => [qw(Getopt)],
81 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
83 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
86 has restart_directory => (
87 traits => [qw(Getopt)],
88 cmd_aliases => [ 'rdir', 'restartdirectory' ],
91 documentation => 'Restarter directory to watch',
92 predicate => '_has_restart_directory',
95 has restart_delay => (
96 traits => [qw(Getopt)],
100 documentation => 'Set a restart delay',
101 predicate => '_has_restart_delay',
105 use Moose::Util::TypeConstraints;
107 my $tc = subtype as RegexpRef;
108 coerce $tc, from Str, via { qr/$_/ };
110 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
112 has restart_regex => (
113 traits => [qw(Getopt)],
118 documentation => 'Restart regex',
119 predicate => '_has_restart_regex',
123 has follow_symlinks => (
124 traits => [qw(Getopt)],
125 cmd_aliases => 'sym',
129 documentation => 'Follow symbolic links',
130 predicate => '_has_follow_symlinks',
133 sub _restarter_args {
138 start_sub => sub { $self->_run_application },
139 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
140 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
141 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
142 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
145 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
149 has restarter_class => (
155 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
162 local $ENV{CATALYST_DEBUG} = 1
165 if ( $self->restart ) {
166 die "Cannot run in the background and also watch for changed files.\n"
167 if $self->background;
169 # If we load this here, then in the case of a restarter, it does not
170 # need to be reloaded for each restart.
173 # If this isn't done, then the Catalyst::Devel tests for the restarter
175 $| = 1 if $ENV{HARNESS_ACTIVE};
177 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
179 my $subclass = $self->restarter_class->pick_subclass;
181 my $restarter = $subclass->new(
182 $self->_restarter_args()
185 $restarter->run_and_watch;
188 $self->_run_application;
194 sub _plack_loader_args {
199 keepalive => $self->keepalive ? 100 : 1,
200 server_ready => sub {
203 my $name = $args->{server_software} || ref($args); # $args is $server
204 my $host = $args->{host} || 0;
205 my $proto = $args->{proto} || 'http';
207 print STDERR "$name: Accepting connections at $proto://$host:$args->{port}/\n";
212 sub _application_args {
219 map { $_ => $self->$_ } qw/
231 __PACKAGE__->meta->make_immutable;
237 Catalyst::Script::Server - Catalyst test server
241 myapp_server.pl [options]
244 -d --debug force debug mode
245 -f --fork handle each request in a new process
247 --help display this help and exits
248 -h --host host (defaults to all)
249 -p --port port (defaults to 3000)
250 -k --keepalive enable keep-alive connections
251 -r --restart restart when files get modified
253 --rd --restart_delay delay between file checks
254 (ignored if you have Linux::Inotify2 installed)
255 --rr --restart_regex regex match files that trigger
256 a restart when modified
257 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
258 --rdir --restart_directory the directory to search for
259 modified files, can be set multiple times
260 (defaults to '[SCRIPT_DIR]/..')
261 --sym --follow_symlinks follow symlinks in search directories
262 (defaults to false. this is a no-op on Win32)
263 --bg --background run the process in the background
264 --pid --pidfile specify filename for pid file
267 perldoc Catalyst::Manual
268 perldoc Catalyst::Manual::Intro
272 Run a Catalyst test server for this application.
276 Catalyst Contributors, see Catalyst.pm
280 This library is free software. You can redistribute it and/or modify
281 it under the same terms as Perl itself.