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