Fix up docs
[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::Moose qw/ArrayRef Str Bool Int/;
10 use namespace::autoclean;
11
12 with 'Catalyst::ScriptRole';
13
14 __PACKAGE__->meta->get_attribute('help')->cmd_aliases('?');
15
16 has debug => (
17     traits => [qw(Getopt)],
18     cmd_aliases => 'd',
19     isa => Bool,
20     is => 'ro',
21     documentation => q{Force debug mode},
22 );
23
24 has host => (
25     traits => [qw(Getopt)],
26     cmd_aliases => 'h',
27     isa => Str,
28     is => 'ro',
29     default => 'localhost',
30     documentation => 'Specify an 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 => Int,
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',
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 has restart_regex => (
105     traits => [qw(Getopt)],
106     cmd_aliases => 'rr',
107     isa => Str,
108     is => 'ro',
109     documentation => 'Restart regex',
110     predicate => '_has_restart_regex',
111 );
112
113 has follow_symlinks => (
114     traits => [qw(Getopt)],
115     cmd_aliases => 'sym',
116     isa => Bool,
117     is => 'ro',
118     default => 0,
119     documentation => 'Follow symbolic links',
120 );
121
122 sub run {
123     my ($self) = shift;
124
125     local $ENV{CATALYST_DEBUG} = 1
126         if $self->debug;
127
128     if ( $self->restart ) {
129         die "Cannot run in the background and also watch for changed files.\n"
130             if $self->background;
131
132         # If we load this here, then in the case of a restarter, it does not
133         # need to be reloaded for each restart.
134         require Catalyst;
135
136         # If this isn't done, then the Catalyst::Devel tests for the restarter
137         # fail.
138         $| = 1 if $ENV{HARNESS_ACTIVE};
139
140         require Catalyst::Restarter;
141
142         my $subclass = Catalyst::Restarter->pick_subclass;
143
144         my %args;
145         $args{follow_symlinks} = $self->follow_symlinks
146             if $self->follow_symlinks;
147         $args{directories}     = $self->restart_directory
148             if $self->_has_restart_directory;
149         $args{sleep_interval}  = $self->restart_delay
150             if $self->_has_restart_delay;
151         $args{filter} = qr/$self->restart_regex/
152             if $self->_has_restart_regex;
153
154         my $restarter = $subclass->new(
155             %args,
156             start_sub => sub { $self->_run_application },
157             argv      => $self->ARGV,
158         );
159
160         $restarter->run_and_watch;
161     }
162     else {
163         $self->_run_application;
164     }
165
166
167 }
168
169 sub _application_args {
170     my ($self) = shift;
171     return (
172         $self->port,
173         $self->host,
174         {
175            map { $_ => $self->$_ } qw/
176                 fork
177                 keepalive
178                 background
179                 pidfile
180                 keepalive
181                 follow_symlinks
182             /,
183         },
184     );
185 }
186
187 __PACKAGE__->meta->make_immutable;
188
189 1;
190
191 =head1 NAME
192
193 Catalyst::Script::Server - Catalyst test server
194
195 =head1 SYNOPSIS
196
197  myapp_server.pl [options]
198
199  Options:
200    -d     --debug          force debug mode
201    -f     --fork           handle each request in a new process
202                       (defaults to false)
203           --help           display this help and exits
204    -h     --host           host (defaults to all)
205    -p     --port           port (defaults to 3000)
206    -k     --keepalive      enable keep-alive connections
207    -r     --restart        restart when files get modified
208                        (defaults to false)
209    --rd   --restartdelay  delay between file checks
210                       (ignored if you have Linux::Inotify2 installed)
211    --rr   --restartregex  regex match files that trigger
212                       a restart when modified
213                       (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
214    --rdir --restartdirectory  the directory to search for
215                       modified files, can be set mulitple times
216                       (defaults to '[SCRIPT_DIR]/..')
217    --sym  --follow_symlinks   follow symlinks in search directories
218                       (defaults to false. this is a no-op on Win32)
219    --bg   --background        run the process in the background
220    --pid  --pidfile           specify filename for pid file
221
222  See also:
223    perldoc Catalyst::Manual
224    perldoc Catalyst::Manual::Intro
225
226 =head1 DESCRIPTION
227
228 Run a Catalyst test server for this application.
229
230 =head1 AUTHORS
231
232 Catalyst Contributors, see Catalyst.pm
233
234 =head1 COPYRIGHT
235
236 This library is free software. You can redistribute it and/or modify
237 it under the same terms as Perl itself.
238
239 =cut