Regression for 5.80015 when rewriting an app, nanonyme++, Khisanth++
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / Server.pm
CommitLineData
0ba6e8aa 1package Catalyst::Script::Server;
2
53b08cf9 3BEGIN {
4 $ENV{CATALYST_ENGINE} ||= 'HTTP';
53b08cf9 5 require Catalyst::Engine::HTTP;
6}
5b923b0b 7
a3ca4468 8use Moose;
883c37ef 9use MooseX::Types::Common::Numeric qw/PositiveInt/;
880d1f8b 10use MooseX::Types::Moose qw/ArrayRef Str Bool Int RegexpRef/;
59804176 11use namespace::autoclean;
a3ca4468 12
d3082fac 13with 'Catalyst::ScriptRole';
a3ca4468 14
3a8c155f 15__PACKAGE__->meta->get_attribute('help')->cmd_aliases('?');
16
f59a9d1b 17has debug => (
4387c692 18 traits => [qw(Getopt)],
19 cmd_aliases => 'd',
20 isa => Bool,
21 is => 'ro',
d3082fac 22 documentation => q{Force debug mode},
e46bf32c 23);
24
57dc50b0 25has host => (
4387c692 26 traits => [qw(Getopt)],
27 cmd_aliases => 'h',
28 isa => Str,
29 is => 'ro',
b1bfeea6 30 # N.B. undef (the default) means we bind on all interfaces on the host.
3bd410a9 31 documentation => 'Specify a hostname or IP on this host for the server to bind to',
41b55019 32);
90b481b1 33
57dc50b0 34has fork => (
4387c692 35 traits => [qw(Getopt)],
36 cmd_aliases => 'f',
37 isa => Bool,
38 is => 'ro',
39 default => 0,
53c6ec79 40 documentation => 'Fork the server to be able to serve multiple requests at once',
90b481b1 41);
42
bc6fa9fc 43has port => (
4387c692 44 traits => [qw(Getopt)],
45 cmd_aliases => 'p',
883c37ef 46 isa => PositiveInt,
4387c692 47 is => 'ro',
48 default => 3000,
53c6ec79 49 documentation => 'Specify a different listening port (to the default port 3000)',
204c6935 50);
51
57dc50b0 52has pidfile => (
4387c692 53 traits => [qw(Getopt)],
54 cmd_aliases => 'pid',
55 isa => Str,
56 is => 'ro',
d3082fac 57 documentation => 'Specify a pidfile',
41b55019 58);
59
57dc50b0 60has keepalive => (
4387c692 61 traits => [qw(Getopt)],
62 cmd_aliases => 'k',
63 isa => Bool,
64 is => 'ro',
65 default => 0,
53c6ec79 66 documentation => 'Support keepalive',
bd31e11f 67);
68
57dc50b0 69has background => (
4387c692 70 traits => [qw(Getopt)],
71 cmd_aliases => 'bg',
72 isa => Bool,
73 is => 'ro',
74 default => 0,
d3082fac 75 documentation => 'Run in the background',
57dc50b0 76);
4b3881d4 77
8f01ed5f 78has restart => (
4387c692 79 traits => [qw(Getopt)],
80 cmd_aliases => 'r',
81 isa => Bool,
82 is => 'ro',
83 default => 0,
53c6ec79 84 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
57dc50b0 85);
a7dea640 86
87has restart_directory => (
4387c692 88 traits => [qw(Getopt)],
1c517146 89 cmd_aliases => [ 'rdir', 'restartdirectory' ],
4387c692 90 isa => ArrayRef[Str],
91 is => 'ro',
d3082fac 92 documentation => 'Restarter directory to watch',
4387c692 93 predicate => '_has_restart_directory',
8f01ed5f 94);
95
57dc50b0 96has restart_delay => (
4387c692 97 traits => [qw(Getopt)],
98 cmd_aliases => 'rd',
99 isa => Int,
100 is => 'ro',
d3082fac 101 documentation => 'Set a restart delay',
4387c692 102 predicate => '_has_restart_delay',
70871584 103);
104
880d1f8b 105{
106 use Moose::Util::TypeConstraints;
107
108 my $tc = subtype as RegexpRef;
109 coerce $tc, from Str, via { qr/$_/ };
110
111 MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
112
113 has restart_regex => (
4387c692 114 traits => [qw(Getopt)],
115 cmd_aliases => 'rr',
116 isa => $tc,
117 coerce => 1,
118 is => 'ro',
880d1f8b 119 documentation => 'Restart regex',
4387c692 120 predicate => '_has_restart_regex',
880d1f8b 121 );
122}
ee7aabd6 123
57dc50b0 124has follow_symlinks => (
4387c692 125 traits => [qw(Getopt)],
126 cmd_aliases => 'sym',
127 isa => Bool,
128 is => 'ro',
129 default => 0,
d3082fac 130 documentation => 'Follow symbolic links',
4387c692 131 predicate => '_has_follow_symlinks',
bbd42ac8 132);
a3ca4468 133
3453b929 134sub _restarter_args {
135 my $self = shift;
34be7d45 136
10255473 137 return (
34be7d45 138 argv => $self->ARGV,
139 start_sub => sub { $self->_run_application },
140 ($self->_has_follow_symlinks ? (follow_symlinks => $self->follow_symlinks) : ()),
141 ($self->_has_restart_delay ? (sleep_interval => $self->restart_delay) : ()),
142 ($self->_has_restart_directory ? (directories => $self->restart_directory) : ()),
880d1f8b 143 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
34be7d45 144 );
3453b929 145}
146
a3ca4468 147sub run {
3453b929 148 my $self = shift;
57dc50b0 149
53c6ec79 150 local $ENV{CATALYST_DEBUG} = 1
151 if $self->debug;
abee32cb 152
a7dea640 153 if ( $self->restart ) {
154 die "Cannot run in the background and also watch for changed files.\n"
155 if $self->background;
156
53c6ec79 157 # If we load this here, then in the case of a restarter, it does not
158 # need to be reloaded for each restart.
159 require Catalyst;
160
161 # If this isn't done, then the Catalyst::Devel tests for the restarter
162 # fail.
163 $| = 1 if $ENV{HARNESS_ACTIVE};
164
a7dea640 165 require Catalyst::Restarter;
166
167 my $subclass = Catalyst::Restarter->pick_subclass;
168
a7dea640 169 my $restarter = $subclass->new(
3453b929 170 $self->_restarter_args()
a7dea640 171 );
172
173 $restarter->run_and_watch;
174 }
175 else {
d3082fac 176 $self->_run_application;
a7dea640 177 }
178
179
57dc50b0 180}
181
d3082fac 182sub _application_args {
a7dea640 183 my ($self) = shift;
d3082fac 184 return (
bc6fa9fc 185 $self->port,
d3082fac 186 $self->host,
57dc50b0 187 {
d3082fac 188 map { $_ => $self->$_ } qw/
189 fork
190 keepalive
191 background
192 pidfile
193 keepalive
194 follow_symlinks
195 /,
196 },
a3ca4468 197 );
a3ca4468 198}
5b923b0b 199
2e81e132 200__PACKAGE__->meta->make_immutable;
5b923b0b 201
0ba6e8aa 2021;
e46bf32c 203
204=head1 NAME
205
cbaaecc0 206Catalyst::Script::Server - Catalyst test server
e46bf32c 207
208=head1 SYNOPSIS
209
cbaaecc0 210 myapp_server.pl [options]
e46bf32c 211
212 Options:
4b3881d4 213 -d --debug force debug mode
214 -f --fork handle each request in a new process
e46bf32c 215 (defaults to false)
3dcfb4c8 216 --help display this help and exits
217 -h --host host (defaults to all)
4b3881d4 218 -p --port port (defaults to 3000)
219 -k --keepalive enable keep-alive connections
220 -r --restart restart when files get modified
221 (defaults to false)
87f5a448 222 --rd --restart_delay delay between file checks
e46bf32c 223 (ignored if you have Linux::Inotify2 installed)
87f5a448 224 --rr --restart_regex regex match files that trigger
e46bf32c 225 a restart when modified
226 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
87f5a448 227 --rdir --restart_directory the directory to search for
e46bf32c 228 modified files, can be set mulitple times
229 (defaults to '[SCRIPT_DIR]/..')
4b3881d4 230 --sym --follow_symlinks follow symlinks in search directories
e46bf32c 231 (defaults to false. this is a no-op on Win32)
4b3881d4 232 --bg --background run the process in the background
233 --pid --pidfile specify filename for pid file
e46bf32c 234
235 See also:
236 perldoc Catalyst::Manual
237 perldoc Catalyst::Manual::Intro
238
239=head1 DESCRIPTION
240
cbaaecc0 241Run a Catalyst test server for this application.
e46bf32c 242
243=head1 AUTHORS
244
245Catalyst Contributors, see Catalyst.pm
246
247=head1 COPYRIGHT
248
249This library is free software. You can redistribute it and/or modify
250it under the same terms as Perl itself.
251
252=cut