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