Move everything back out into the scriptrole, allow the scripts to force their engine..
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Script / Server.pm
CommitLineData
0ba6e8aa 1package Catalyst::Script::Server;
a3ca4468 2use Moose;
883c37ef 3use MooseX::Types::Common::Numeric qw/PositiveInt/;
880d1f8b 4use MooseX::Types::Moose qw/ArrayRef Str Bool Int RegexpRef/;
b89d8b98 5use Catalyst::Utils;
59804176 6use namespace::autoclean;
a3ca4468 7
c821df21 8sub _plack_engine_name { 'Standalone' }
9
d3082fac 10with 'Catalyst::ScriptRole';
a3ca4468 11
3a8c155f 12__PACKAGE__->meta->get_attribute('help')->cmd_aliases('?');
13
f59a9d1b 14has debug => (
4387c692 15 traits => [qw(Getopt)],
16 cmd_aliases => 'd',
17 isa => Bool,
18 is => 'ro',
d3082fac 19 documentation => q{Force debug mode},
e46bf32c 20);
21
57dc50b0 22has host => (
4387c692 23 traits => [qw(Getopt)],
24 cmd_aliases => 'h',
25 isa => Str,
26 is => 'ro',
b1bfeea6 27 # N.B. undef (the default) means we bind on all interfaces on the host.
3bd410a9 28 documentation => 'Specify a hostname or IP on this host for the server to bind to',
41b55019 29);
90b481b1 30
57dc50b0 31has fork => (
4387c692 32 traits => [qw(Getopt)],
33 cmd_aliases => 'f',
34 isa => Bool,
35 is => 'ro',
36 default => 0,
53c6ec79 37 documentation => 'Fork the server to be able to serve multiple requests at once',
90b481b1 38);
39
bc6fa9fc 40has port => (
4387c692 41 traits => [qw(Getopt)],
42 cmd_aliases => 'p',
883c37ef 43 isa => PositiveInt,
4387c692 44 is => 'ro',
b89d8b98 45 default => sub {
56eb9db4 46 Catalyst::Utils::env_value(shift->application_name, 'port') || 3000
b89d8b98 47 },
53c6ec79 48 documentation => 'Specify a different listening port (to the default port 3000)',
204c6935 49);
50
57dc50b0 51has pidfile => (
4387c692 52 traits => [qw(Getopt)],
53 cmd_aliases => 'pid',
54 isa => Str,
55 is => 'ro',
d3082fac 56 documentation => 'Specify a pidfile',
41b55019 57);
58
57dc50b0 59has keepalive => (
4387c692 60 traits => [qw(Getopt)],
61 cmd_aliases => 'k',
62 isa => Bool,
63 is => 'ro',
64 default => 0,
53c6ec79 65 documentation => 'Support keepalive',
bd31e11f 66);
67
57dc50b0 68has background => (
4387c692 69 traits => [qw(Getopt)],
70 cmd_aliases => 'bg',
71 isa => Bool,
72 is => 'ro',
73 default => 0,
d3082fac 74 documentation => 'Run in the background',
57dc50b0 75);
4b3881d4 76
8f01ed5f 77has restart => (
4387c692 78 traits => [qw(Getopt)],
79 cmd_aliases => 'r',
80 isa => Bool,
81 is => 'ro',
56eb9db4 82 default => sub {
83 Catalyst::Utils::env_value(shift->application_name, 'reload') || 0;
84 },
53c6ec79 85 documentation => 'use Catalyst::Restarter to detect code changes and restart the application',
57dc50b0 86);
a7dea640 87
88has restart_directory => (
4387c692 89 traits => [qw(Getopt)],
1c517146 90 cmd_aliases => [ 'rdir', 'restartdirectory' ],
4387c692 91 isa => ArrayRef[Str],
92 is => 'ro',
d3082fac 93 documentation => 'Restarter directory to watch',
4387c692 94 predicate => '_has_restart_directory',
8f01ed5f 95);
96
57dc50b0 97has restart_delay => (
4387c692 98 traits => [qw(Getopt)],
99 cmd_aliases => 'rd',
100 isa => Int,
101 is => 'ro',
d3082fac 102 documentation => 'Set a restart delay',
4387c692 103 predicate => '_has_restart_delay',
70871584 104);
105
880d1f8b 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 => (
4387c692 115 traits => [qw(Getopt)],
116 cmd_aliases => 'rr',
117 isa => $tc,
118 coerce => 1,
119 is => 'ro',
880d1f8b 120 documentation => 'Restart regex',
4387c692 121 predicate => '_has_restart_regex',
880d1f8b 122 );
123}
ee7aabd6 124
57dc50b0 125has follow_symlinks => (
4387c692 126 traits => [qw(Getopt)],
127 cmd_aliases => 'sym',
128 isa => Bool,
129 is => 'ro',
130 default => 0,
d3082fac 131 documentation => 'Follow symbolic links',
4387c692 132 predicate => '_has_follow_symlinks',
bbd42ac8 133);
a3ca4468 134
3453b929 135sub _restarter_args {
136 my $self = shift;
34be7d45 137
10255473 138 return (
34be7d45 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) : ()),
880d1f8b 144 ($self->_has_restart_regex ? (filter => $self->restart_regex) : ()),
34be7d45 145 );
3453b929 146}
147
a3ca4468 148sub run {
3453b929 149 my $self = shift;
57dc50b0 150
53c6ec79 151 local $ENV{CATALYST_DEBUG} = 1
152 if $self->debug;
abee32cb 153
a7dea640 154 if ( $self->restart ) {
155 die "Cannot run in the background and also watch for changed files.\n"
156 if $self->background;
157
53c6ec79 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
a7dea640 166 require Catalyst::Restarter;
167
168 my $subclass = Catalyst::Restarter->pick_subclass;
169
a7dea640 170 my $restarter = $subclass->new(
3453b929 171 $self->_restarter_args()
a7dea640 172 );
173
174 $restarter->run_and_watch;
175 }
176 else {
d3082fac 177 $self->_run_application;
a7dea640 178 }
179
180
57dc50b0 181}
182
d3082fac 183sub _application_args {
a7dea640 184 my ($self) = shift;
d3082fac 185 return (
bc6fa9fc 186 $self->port,
d3082fac 187 $self->host,
57dc50b0 188 {
d3082fac 189 map { $_ => $self->$_ } qw/
190 fork
191 keepalive
192 background
193 pidfile
194 keepalive
195 follow_symlinks
196 /,
197 },
a3ca4468 198 );
a3ca4468 199}
5b923b0b 200
2e81e132 201__PACKAGE__->meta->make_immutable;
5b923b0b 202
0ba6e8aa 2031;
e46bf32c 204
205=head1 NAME
206
cbaaecc0 207Catalyst::Script::Server - Catalyst test server
e46bf32c 208
209=head1 SYNOPSIS
210
cbaaecc0 211 myapp_server.pl [options]
e46bf32c 212
213 Options:
4b3881d4 214 -d --debug force debug mode
215 -f --fork handle each request in a new process
e46bf32c 216 (defaults to false)
3dcfb4c8 217 --help display this help and exits
218 -h --host host (defaults to all)
4b3881d4 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)
87f5a448 223 --rd --restart_delay delay between file checks
e46bf32c 224 (ignored if you have Linux::Inotify2 installed)
87f5a448 225 --rr --restart_regex regex match files that trigger
e46bf32c 226 a restart when modified
227 (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
87f5a448 228 --rdir --restart_directory the directory to search for
e46bf32c 229 modified files, can be set mulitple times
230 (defaults to '[SCRIPT_DIR]/..')
4b3881d4 231 --sym --follow_symlinks follow symlinks in search directories
e46bf32c 232 (defaults to false. this is a no-op on Win32)
4b3881d4 233 --bg --background run the process in the background
234 --pid --pidfile specify filename for pid file
e46bf32c 235
236 See also:
237 perldoc Catalyst::Manual
238 perldoc Catalyst::Manual::Intro
239
240=head1 DESCRIPTION
241
cbaaecc0 242Run a Catalyst test server for this application.
e46bf32c 243
244=head1 AUTHORS
245
246Catalyst Contributors, see Catalyst.pm
247
248=head1 COPYRIGHT
249
250This library is free software. You can redistribute it and/or modify
251it under the same terms as Perl itself.
252
253=cut