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