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