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