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