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