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