added documentation, surely needs refining
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / Server.pm
1 package Catalyst::Script::Server;
2
3 BEGIN {
4     $ENV{CATALYST_ENGINE} ||= 'HTTP';
5     $ENV{CATALYST_SCRIPT_GEN} = 31;
6     require Catalyst::Engine::HTTP;
7 }
8
9 use FindBin qw/$Bin/;
10 use lib "$Bin/../lib";
11 use Pod::Usage;
12 use Moose;
13 use Catalyst::Restarter;
14 use namespace::autoclean;
15
16 with 'MooseX::Getopt';
17
18 has debug => (
19     traits => [qw(Getopt)],
20     cmd_aliases => 'd',
21     isa => 'Bool',
22     is => 'ro',
23     documentation => qq{ force debug mode }
24
25 );
26
27 has help => (
28     traits => [qw(Getopt)],
29     cmd_aliases => 'h',
30     isa => 'Bool',
31     is => 'ro',
32     documentation => qq{ display this help and exits },
33 );
34
35 has host => (
36     isa => 'Str',
37     is => 'ro',
38     default =>  "localhost",
39     documentation => qq{ specify a host for the server to run on }
40 );
41
42 has fork => (
43     traits => [qw(Getopt)],
44     cmd_aliases => 'f',
45     isa => 'Bool',
46     is => 'ro',
47     documentation => qq{ fork the server }
48 );
49
50 has listen => (
51     traits => [qw(Getopt)],
52     cmd_aliases => 'l',
53     isa => 'Int',
54     is => 'ro',
55     default => "3000",
56     documentation => qq{ specify a different listening port }
57 );
58
59 has pidfile => (
60     traits => [qw(Getopt)],
61     cmd_aliases => 'pid',
62     isa => 'Str',
63     is => 'ro',
64     documentation => qq{ specify a pidfile }
65 );
66
67 has keepalive => (
68     traits => [qw(Getopt)],
69     cmd_aliases => 'k',
70     isa => 'Bool',
71     is => 'ro',
72     documentation => qq{ server keepalive },
73
74 );
75
76 has background => (
77     traits => [qw(Getopt)],
78     cmd_aliases => 'bg',
79     isa => 'Bool',
80     is => 'ro',
81     documentation => qq{ run in the background }
82 );
83
84
85 has _app => (
86     reader   => 'app',
87     init_arg => 'app',
88     traits => [qw(NoGetopt)],
89     isa => 'Str',
90     is => 'ro',
91 );
92
93 has restart => (
94     traits => [qw(Getopt)],
95     cmd_aliases => 'r',
96     isa => 'Bool',
97     is => 'ro',
98     documentation => qq{ use Catalyst::Restarter to detect code changes }
99 );
100
101 has restart_directory => (
102     traits => [qw(Getopt)],
103     cmd_aliases => 'rdir',
104     isa => 'ArrayRef[Str]',
105     is  => 'ro',
106     predicate => '_has_restart_directory',
107     documentation => qq{ restarter directory to watch }
108 );
109
110 has restart_delay => (
111     traits => [qw(Getopt)],
112     cmd_aliases => 'rdel',
113     isa => 'Int',
114     is => 'ro',
115     predicate => '_has_restart_delay',
116     documentation => qq{ set a restart delay }
117 );
118
119 has restart_regex => (
120     traits => [qw(Getopt)],
121     cmd_aliases => 'rxp',
122     isa => 'Str',
123     is => 'ro',
124     predicate => '_has_restart_regex',
125     documentation => qq{ restart regex }
126 );
127
128 has follow_symlinks => (
129     traits => [qw(Getopt)],
130     cmd_aliases => 'sym',
131     isa => 'Bool',
132     is => 'ro',
133     predicate => '_has_follow_symlinks',
134     documentation => qq{ follow symbolic links }
135
136 );
137
138 sub usage {
139     my ($self) = shift;
140
141     return pod2usage();
142
143 }
144
145
146 sub run {
147     my ($self) = shift;
148
149     $self->usage if $self->help;
150
151     if ( $self->debug ) {
152         $ENV{CATALYST_DEBUG} = 1;
153     }
154
155     # If we load this here, then in the case of a restarter, it does not
156     # need to be reloaded for each restart.
157     require Catalyst;
158
159     # If this isn't done, then the Catalyst::Devel tests for the restarter
160     # fail.
161     $| = 1 if $ENV{HARNESS_ACTIVE};
162
163     if ( $self->restart ) {
164         die "Cannot run in the background and also watch for changed files.\n"
165             if $self->background;
166
167         require Catalyst::Restarter;
168
169         my $subclass = Catalyst::Restarter->pick_subclass;
170
171         my %args;
172         $args{follow_symlinks} = $self->follow_symlinks
173             if $self->_has_follow_symlinks;
174         $args{directories}     = $self->restart_directory
175             if $self->_has_restart_directory;
176         $args{sleep_interval}  = $self->restart_delay
177             if $self->_has_restart_delay;
178         $args{filter} = qr/$self->restart_regex/
179             if $self->_has_restart_regex;
180
181         my $restarter = $subclass->new(
182             %args,
183             start_sub => sub { $self->_run },
184             argv      => \$self->ARGV,
185         );
186
187         $restarter->run_and_watch;
188     }
189     else {
190         $self->_run;
191     }
192
193
194 }
195
196 sub _run {
197     my ($self) = shift;
198
199     my $app = $self->app;
200     Class::MOP::load_class($app);
201
202     $app->run(
203         $self->listen, $self->host,
204         {
205            'fork'            => $self->fork,
206            keepalive         => $self->keepalive,
207            background        => $self->background,
208            pidfile           => $self->pidfile,
209            keepalive         => $self->keepalive,
210            follow_symlinks   => $self->follow_symlinks,
211         }
212     );
213 }
214
215
216 no Moose;
217 __PACKAGE__->meta->make_immutable;
218
219 1;
220
221 =head1 NAME
222
223 [% appprefix %]_server.pl - Catalyst Testserver
224
225 =head1 SYNOPSIS
226
227 [% appprefix %]_server.pl [options]
228
229  Options:
230    -d     --debug          force debug mode
231    -f     --fork           handle each request in a new process
232                       (defaults to false)
233    -h     --help           display this help and exits
234           --host           host (defaults to all)
235    -p     --port           port (defaults to 3000)
236    -k     --keepalive      enable keep-alive connections
237    -r     --restart        restart when files get modified
238                        (defaults to false)
239    --rd   --restartdelay  delay between file checks
240                       (ignored if you have Linux::Inotify2 installed)
241    --rr   --restartregex  regex match files that trigger
242                       a restart when modified
243                       (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
244    --rdir --restartdirectory  the directory to search for
245                       modified files, can be set mulitple times
246                       (defaults to '[SCRIPT_DIR]/..')
247    --sym  --follow_symlinks   follow symlinks in search directories
248                       (defaults to false. this is a no-op on Win32)
249    --bg   --background        run the process in the background
250    --pid  --pidfile           specify filename for pid file
251
252  See also:
253    perldoc Catalyst::Manual
254    perldoc Catalyst::Manual::Intro
255
256 =head1 DESCRIPTION
257
258 Run a Catalyst Testserver for this application.
259
260 =head1 AUTHORS
261
262 Catalyst Contributors, see Catalyst.pm
263
264 =head1 COPYRIGHT
265
266 This library is free software. You can redistribute it and/or modify
267 it under the same terms as Perl itself.
268
269 =cut