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