Move everything back out into the scriptrole, allow the scripts to force their engine..
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / Server.pm
1 package Catalyst::Script::Server;
2 use Moose;
3 use MooseX::Types::Common::Numeric qw/PositiveInt/;
4 use MooseX::Types::Moose qw/ArrayRef Str Bool Int RegexpRef/;
5 use Catalyst::Utils;
6 use namespace::autoclean;
7
8 sub _plack_engine_name { 'Standalone' }
9
10 with 'Catalyst::ScriptRole';
11
12 __PACKAGE__->meta->get_attribute('help')->cmd_aliases('?');
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     # N.B. undef (the default) means we bind on all interfaces on the host.
28     documentation => 'Specify a hostname or 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           => PositiveInt,
44     is            => 'ro',
45     default       => sub {
46         Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
47     },
48     documentation => 'Specify a different listening port (to the default port 3000)',
49 );
50
51 has pidfile => (
52     traits        => [qw(Getopt)],
53     cmd_aliases   => 'pid',
54     isa           => Str,
55     is            => 'ro',
56     documentation => 'Specify a pidfile',
57 );
58
59 has keepalive => (
60     traits        => [qw(Getopt)],
61     cmd_aliases   => 'k',
62     isa           => Bool,
63     is            => 'ro',
64     default       => 0,
65     documentation => 'Support keepalive',
66 );
67
68 has background => (
69     traits        => [qw(Getopt)],
70     cmd_aliases   => 'bg',
71     isa           => Bool,
72     is            => 'ro',
73     default       => 0,
74     documentation => 'Run in the background',
75 );
76
77 has restart => (
78     traits        => [qw(Getopt)],
79     cmd_aliases   => 'r',
80     isa           => Bool,
81     is            => 'ro',
82     default       => sub {
83         Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
84     },
85     documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
86 );
87
88 has restart_directory => (
89     traits        => [qw(Getopt)],
90     cmd_aliases   => [ 'rdir', 'restartdirectory' ],
91     isa           => ArrayRef[Str],
92     is            => 'ro',
93     documentation => 'Restarter directory to watch',
94     predicate     => '_has_restart_directory',
95 );
96
97 has restart_delay => (
98     traits        => [qw(Getopt)],
99     cmd_aliases   => 'rd',
100     isa           => Int,
101     is            => 'ro',
102     documentation => 'Set a restart delay',
103     predicate     => '_has_restart_delay',
104 );
105
106 {
107     use Moose::Util::TypeConstraints;
108
109     my $tc = subtype as RegexpRef;
110     coerce $tc, from Str, via { qr/$_/ };
111
112     MooseX::Getopt::OptionTypeMap->add_option_type_to_map($tc => '=s');
113
114     has restart_regex => (
115         traits        => [qw(Getopt)],
116         cmd_aliases   => 'rr',
117         isa           => $tc,
118         coerce        => 1,
119         is            => 'ro',
120         documentation => 'Restart regex',
121         predicate     => '_has_restart_regex',
122     );
123 }
124
125 has follow_symlinks => (
126     traits        => [qw(Getopt)],
127     cmd_aliases   => 'sym',
128     isa           => Bool,
129     is            => 'ro',
130     default       => 0,
131     documentation => 'Follow symbolic links',
132     predicate     => '_has_follow_symlinks',
133 );
134
135 sub _restarter_args {
136     my $self = shift;
137
138     return (
139         argv => $self->ARGV,
140         start_sub => sub { $self->_run_application },
141         ($self->_has_follow_symlinks   ? (follow_symlinks => $self->follow_symlinks)   : ()),
142         ($self->_has_restart_delay     ? (sleep_interval  => $self->restart_delay)     : ()),
143         ($self->_has_restart_directory ? (directories     => $self->restart_directory) : ()),
144         ($self->_has_restart_regex     ? (filter          => $self->restart_regex)     : ()),
145     );
146 }
147
148 sub run {
149     my $self = shift;
150
151     local $ENV{CATALYST_DEBUG} = 1
152         if $self->debug;
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         # If we load this here, then in the case of a restarter, it does not
159         # need to be reloaded for each restart.
160         require Catalyst;
161
162         # If this isn't done, then the Catalyst::Devel tests for the restarter
163         # fail.
164         $| = 1 if $ENV{HARNESS_ACTIVE};
165
166         require Catalyst::Restarter;
167
168         my $subclass = Catalyst::Restarter->pick_subclass;
169
170         my $restarter = $subclass->new(
171             $self->_restarter_args()
172         );
173
174         $restarter->run_and_watch;
175     }
176     else {
177         $self->_run_application;
178     }
179
180
181 }
182
183 sub _application_args {
184     my ($self) = shift;
185     return (
186         $self->port,
187         $self->host,
188         {
189            map { $_ => $self->$_ } qw/
190                 fork
191                 keepalive
192                 background
193                 pidfile
194                 keepalive
195                 follow_symlinks
196             /,
197         },
198     );
199 }
200
201 __PACKAGE__->meta->make_immutable;
202
203 1;
204
205 =head1 NAME
206
207 Catalyst::Script::Server - Catalyst test server
208
209 =head1 SYNOPSIS
210
211  myapp_server.pl [options]
212
213  Options:
214    -d     --debug          force debug mode
215    -f     --fork           handle each request in a new process
216                       (defaults to false)
217           --help           display this help and exits
218    -h     --host           host (defaults to all)
219    -p     --port           port (defaults to 3000)
220    -k     --keepalive      enable keep-alive connections
221    -r     --restart        restart when files get modified
222                        (defaults to false)
223    --rd   --restart_delay  delay between file checks
224                       (ignored if you have Linux::Inotify2 installed)
225    --rr   --restart_regex  regex match files that trigger
226                       a restart when modified
227                       (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
228    --rdir --restart_directory  the directory to search for
229                       modified files, can be set mulitple times
230                       (defaults to '[SCRIPT_DIR]/..')
231    --sym  --follow_symlinks   follow symlinks in search directories
232                       (defaults to false. this is a no-op on Win32)
233    --bg   --background        run the process in the background
234    --pid  --pidfile           specify filename for pid file
235
236  See also:
237    perldoc Catalyst::Manual
238    perldoc Catalyst::Manual::Intro
239
240 =head1 DESCRIPTION
241
242 Run a Catalyst test server for this application.
243
244 =head1 AUTHORS
245
246 Catalyst Contributors, see Catalyst.pm
247
248 =head1 COPYRIGHT
249
250 This library is free software. You can redistribute it and/or modify
251 it under the same terms as Perl itself.
252
253 =cut