Don't default to localhost for --host
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / Server.pm
1 package Catalyst::Script::Server;
2
3 BEGIN {
4     $ENV{CATALYST_ENGINE} ||= 'HTTP';
5     require Catalyst::Engine::HTTP;
6 }
7
8 use Moose;
9 use MooseX::Types::Common::Numeric qw/PositiveInt/;
10 use MooseX::Types::Moose qw/ArrayRef Str Bool Int RegexpRef/;
11 use namespace::autoclean;
12
13 with 'Catalyst::ScriptRole';
14
15 __PACKAGE__->meta->get_attribute('help')->cmd_aliases('?');
16
17 has debug => (
18     traits        => [qw(Getopt)],
19     cmd_aliases   => 'd',
20     isa           => Bool,
21     is            => 'ro',
22     documentation => q{Force debug mode},
23 );
24
25 has host => (
26     traits        => [qw(Getopt)],
27     cmd_aliases   => 'h',
28     isa           => Str,
29     is            => 'ro',
30     documentation => 'Specify a hostname or IP on this host for the server to bind to',
31 );
32
33 has fork => (
34     traits        => [qw(Getopt)],
35     cmd_aliases   => 'f',
36     isa           => Bool,
37     is            => 'ro',
38     default       => 0,
39     documentation => 'Fork the server to be able to serve multiple requests at once',
40 );
41
42 has port => (
43     traits        => [qw(Getopt)],
44     cmd_aliases   => 'p',
45     isa           => PositiveInt,
46     is            => 'ro',
47     default       => 3000,
48     documentation => 'Specify a different listening port (to the default port 3000)',
49 );
50
51 has pidfile => (
52     traits        => [qw(Getopt)],
53     cmd_aliases   => 'pid',
54     isa           => Str,
55     is            => 'ro',
56     documentation => 'Specify a pidfile',
57 );
58
59 has keepalive => (
60     traits        => [qw(Getopt)],
61     cmd_aliases   => 'k',
62     isa           => Bool,
63     is            => 'ro',
64     default       => 0,
65     documentation => 'Support keepalive',
66 );
67
68 has background => (
69     traits        => [qw(Getopt)],
70     cmd_aliases   => 'bg',
71     isa           => Bool,
72     is            => 'ro',
73     default       => 0,
74     documentation => 'Run in the background',
75 );
76
77 has restart => (
78     traits        => [qw(Getopt)],
79     cmd_aliases   => 'r',
80     isa           => Bool,
81     is            => 'ro',
82     default       => 0,
83     documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
84 );
85
86 has restart_directory => (
87     traits        => [qw(Getopt)],
88     cmd_aliases   => [ 'rdir', 'restartdirectory' ],
89     isa           => ArrayRef[Str],
90     is            => 'ro',
91     documentation => 'Restarter directory to watch',
92     predicate     => '_has_restart_directory',
93 );
94
95 has restart_delay => (
96     traits        => [qw(Getopt)],
97     cmd_aliases   => 'rd',
98     isa           => Int,
99     is            => 'ro',
100     documentation => 'Set a restart delay',
101     predicate     => '_has_restart_delay',
102 );
103
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 }
122
123 has follow_symlinks => (
124     traits        => [qw(Getopt)],
125     cmd_aliases   => 'sym',
126     isa           => Bool,
127     is            => 'ro',
128     default       => 0,
129     documentation => 'Follow symbolic links',
130     predicate     => '_has_follow_symlinks',
131 );
132
133 sub _restarter_args {
134     my $self = shift;
135
136     return (
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) : ()),
142         ($self->_has_restart_regex     ? (filter          => $self->restart_regex)     : ()),
143     );
144 }
145
146 sub run {
147     my $self = shift;
148
149     local $ENV{CATALYST_DEBUG} = 1
150         if $self->debug;
151
152     if ( $self->restart ) {
153         die "Cannot run in the background and also watch for changed files.\n"
154             if $self->background;
155
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
164         require Catalyst::Restarter;
165
166         my $subclass = Catalyst::Restarter->pick_subclass;
167
168         my $restarter = $subclass->new(
169             $self->_restarter_args()
170         );
171
172         $restarter->run_and_watch;
173     }
174     else {
175         $self->_run_application;
176     }
177
178
179 }
180
181 sub _application_args {
182     my ($self) = shift;
183     return (
184         $self->port,
185         $self->host,
186         {
187            map { $_ => $self->$_ } qw/
188                 fork
189                 keepalive
190                 background
191                 pidfile
192                 keepalive
193                 follow_symlinks
194             /,
195         },
196     );
197 }
198
199 __PACKAGE__->meta->make_immutable;
200
201 1;
202
203 =head1 NAME
204
205 Catalyst::Script::Server - Catalyst test server
206
207 =head1 SYNOPSIS
208
209  myapp_server.pl [options]
210
211  Options:
212    -d     --debug          force debug mode
213    -f     --fork           handle each request in a new process
214                       (defaults to false)
215           --help           display this help and exits
216    -h     --host           host (defaults to all)
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)
221    --rd   --restart_delay  delay between file checks
222                       (ignored if you have Linux::Inotify2 installed)
223    --rr   --restart_regex  regex match files that trigger
224                       a restart when modified
225                       (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
226    --rdir --restart_directory  the directory to search for
227                       modified files, can be set mulitple times
228                       (defaults to '[SCRIPT_DIR]/..')
229    --sym  --follow_symlinks   follow symlinks in search directories
230                       (defaults to false. this is a no-op on Win32)
231    --bg   --background        run the process in the background
232    --pid  --pidfile           specify filename for pid file
233
234  See also:
235    perldoc Catalyst::Manual
236    perldoc Catalyst::Manual::Intro
237
238 =head1 DESCRIPTION
239
240 Run a Catalyst test server for this application.
241
242 =head1 AUTHORS
243
244 Catalyst Contributors, see Catalyst.pm
245
246 =head1 COPYRIGHT
247
248 This library is free software. You can redistribute it and/or modify
249 it under the same terms as Perl itself.
250
251 =cut