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