fasterized
[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::Engine::HTTP;
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{
24     -d --debug force debug mode    
25     }
26
27 );
28
29 has help => ( 
30     traits => [qw(Getopt)],
31     cmd_aliases => 'h',
32     isa => 'Bool',   
33     is => 'ro', 
34     documentation => qq{
35     -h --help display this help and exits    
36     },  
37 );
38
39 has host => ( 
40     isa => 'Str',    
41     is => 'ro', 
42     , 
43     default =>  "localhost" 
44 );
45
46 has fork => ( 
47     traits => [qw(Getopt)],
48     cmd_aliases => 'f',
49     isa => 'Bool',
50     is => 'ro', 
51      
52 );
53
54 has listen => ( 
55     traits => [qw(Getopt)],
56     cmd_aliases => 'l',
57     isa => 'Int',
58     is => 'ro', 
59     , 
60     default => "3000" 
61 );
62
63 has pidfile => ( 
64     traits => [qw(Getopt)],
65     cmd_aliases => 'pid',
66     isa => 'Str',    
67     is => 'ro', 
68      
69 );
70
71 has keepalive => ( 
72     traits => [qw(Getopt)],
73     cmd_aliases => 'k',
74     isa => 'Bool',   
75     is => 'ro', 
76     , 
77      
78 );
79
80 has background => ( 
81     traits => [qw(Getopt)],
82     cmd_aliases => 'bg',
83     isa => 'Bool',   
84     is => 'ro', 
85 );
86
87
88 has _app => ( 
89     reader   => 'app', 
90     init_arg => 'app',
91     traits => [qw(NoGetopt)],
92     isa => 'Str',    
93     is => 'ro', 
94 ); 
95
96 has restart => (
97     traits => [qw(Getopt)],
98     cmd_aliases => 'r', 
99     isa => 'Bool',   
100     is => 'ro', 
101      
102 );
103
104 has restart_delay => ( 
105     traits => [qw(Getopt)],
106     cmd_aliases => 'rdel',
107     isa => 'Int',    
108     is => 'ro', 
109      
110 );
111
112 has restart_regex => ( 
113     traits => [qw(Getopt)],
114     cmd_aliases => 'rxp',
115     isa => 'Str',    
116     is => 'ro', 
117      
118 );
119
120 has follow_symlinks => ( 
121     traits => [qw(Getopt)],
122     cmd_aliases => 'sym',
123     isa => 'Bool',   
124     is => 'ro', 
125      
126 );
127
128 sub usage {
129     my ($self) = shift;
130     
131     return pod2usage();
132
133 }
134
135 my @argv = @ARGV;
136
137 sub run {
138     my $self = shift;
139     
140     $self->usage if $self->help;
141     my $app = $self->app;
142     Class::MOP::load_class($app);
143     $app->run(
144         $self->listen, $self->host,
145         {  
146            'fork'            => $self->fork,
147            keepalive         => $self->keepalive,
148            background        => $self->background,
149            pidfile           => $self->pidfile,
150            keepalive         => $self->keepalive,
151            restart           => $self->restart,
152            restart_delay     => $self->restart_delay,
153            restart_regex     => qr/$self->restart_regex/,
154 # FIXME    restart_directory => $self->restart_directory,
155            follow_symlinks   => $self->follow_symlinks,
156         }  
157     );
158
159 }
160
161 no Moose;
162 __PACKAGE__->meta->make_immutable;
163
164 1;
165
166 =head1 NAME
167
168 [% appprefix %]_server.pl - Catalyst Testserver
169
170 =head1 SYNOPSIS
171
172 [% appprefix %]_server.pl [options]
173
174  Options:
175    -d     --debug          force debug mode
176    -f     --fork           handle each request in a new process
177                       (defaults to false)
178    -h     --help           display this help and exits
179           --host           host (defaults to all)
180    -p     --port           port (defaults to 3000)
181    -k     --keepalive      enable keep-alive connections
182    -r     --restart        restart when files get modified
183                        (defaults to false)
184    --rd   --restartdelay  delay between file checks
185                       (ignored if you have Linux::Inotify2 installed)
186    --rr   --restartregex  regex match files that trigger
187                       a restart when modified
188                       (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
189    --rdir --restartdirectory  the directory to search for
190                       modified files, can be set mulitple times
191                       (defaults to '[SCRIPT_DIR]/..')
192    --sym  --follow_symlinks   follow symlinks in search directories
193                       (defaults to false. this is a no-op on Win32)
194    --bg   --background        run the process in the background
195    --pid  --pidfile           specify filename for pid file
196
197  See also:
198    perldoc Catalyst::Manual
199    perldoc Catalyst::Manual::Intro
200
201 =head1 DESCRIPTION
202
203 Run a Catalyst Testserver for this application.
204
205 =head1 AUTHORS
206
207 Catalyst Contributors, see Catalyst.pm
208
209 =head1 COPYRIGHT
210
211 This library is free software. You can redistribute it and/or modify
212 it under the same terms as Perl itself.
213
214 =cut