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