1 package Catalyst::Script::Server;
4 $ENV{CATALYST_ENGINE} ||= 'HTTP';
5 require Catalyst::Engine::HTTP;
9 use MooseX::Types::Common::Numeric qw/PositiveInt/;
10 use MooseX::Types::Moose qw/ArrayRef Str Bool Int RegexpRef/;
12 use namespace::autoclean;
14 with 'Catalyst::ScriptRole';
17 traits => [qw(Getopt)],
21 documentation => q{Force debug mode},
25 traits => [qw(Getopt)],
29 # N.B. undef (the default) means we bind on all interfaces on the host.
30 documentation => 'Specify a hostname or IP on this host for the server to bind to',
34 traits => [qw(Getopt)],
39 documentation => 'Fork the server to be able to serve multiple requests at once',
43 traits => [qw(Getopt)],
48 Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
50 documentation => 'Specify a different listening port (to the default port 3000)',
54 traits => [qw(Getopt)],
58 documentation => 'Specify a pidfile',
62 traits => [qw(Getopt)],
67 documentation => 'Support keepalive',
71 traits => [qw(Getopt)],
76 documentation => 'Run in the background',
80 traits => [qw(Getopt)],
85 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
87 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
90 has restart_directory => (
91 traits => [qw(Getopt)],
92 cmd_aliases => [ 'rdir', 'restartdirectory' ],
95 documentation => 'Restarter directory to watch',
96 predicate => '_has_restart_directory',
99 has restart_delay => (
100 traits => [qw(Getopt)],
104 documentation => 'Set a restart delay',
105 predicate => '_has_restart_delay',
109 use Moose::Util::TypeConstraints;
111 my $tc = subtype as RegexpRef;
112 coerce $tc, from Str, via { qr/$_/ };
114 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
116 has restart_regex => (
117 traits => [qw(Getopt)],
122 documentation => 'Restart regex',
123 predicate => '_has_restart_regex',
127 has follow_symlinks => (
128 traits => [qw(Getopt)],
129 cmd_aliases => 'sym',
133 documentation => 'Follow symbolic links',
134 predicate => '_has_follow_symlinks',
137 sub _restarter_args {
142 start_sub => sub { $self->_run_application },
143 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
144 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
145 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
146 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
149 map { $_ => $self->$_ } qw(application_name host port debug pidfile fork background keepalive)
153 has restarter_class => (
159 Catalyst::Utils::env_value($self->application_name, 'RESTARTER') || 'Catalyst::Restarter';
166 local $ENV{CATALYST_DEBUG} = 1
169 if ( $self->restart ) {
170 die "Cannot run in the background and also watch for changed files.\n"
171 if $self->background;
173 # If we load this here, then in the case of a restarter, it does not
174 # need to be reloaded for each restart.
177 # If this isn't done, then the Catalyst::Devel tests for the restarter
179 $| = 1 if $ENV{HARNESS_ACTIVE};
181 Catalyst::Utils::ensure_class_loaded($self->restarter_class);
183 my $subclass = $self->restarter_class->pick_subclass;
185 my $restarter = $subclass->new(
186 $self->_restarter_args()
189 $restarter->run_and_watch;
192 $self->_run_application;
198 sub _application_args {
205 map { $_ => $self->$_ } qw/
217 __PACKAGE__->meta->make_immutable;
223 Catalyst::Script::Server - Catalyst test server
227 myapp_server.pl [options]
230 -d --debug force debug mode
231 -f --fork handle each request in a new process
233 --help display this help and exits
234 -h --host host (defaults to all)
235 -p --port port (defaults to 3000)
236 -k --keepalive enable keep-alive connections
237 -r --restart restart when files get modified
239 --rd --restart_delay delay between file checks
240 (ignored if you have Linux::Inotify2 installed)
241 --rr --restart_regex regex match files that trigger
242 a restart when modified
243 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
244 --rdir --restart_directory the directory to search for
245 modified files, can be set multiple times
246 (defaults to '[SCRIPT_DIR]/..')
247 --sym --follow_symlinks follow symlinks in search directories
248 (defaults to false. this is a no-op on Win32)
249 --bg --background run the process in the background
250 --pid --pidfile specify filename for pid file
253 perldoc Catalyst::Manual
254 perldoc Catalyst::Manual::Intro
258 Run a Catalyst test server for this application.
262 Catalyst Contributors, see Catalyst.pm
266 This library is free software. You can redistribute it and/or modify
267 it under the same terms as Perl itself.