File Coverage

blib/lib/FCGI/Engine.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package FCGI::Engine;
2 1     1   28148 use Moose;
  0            
  0            
3              
4             use Class::Load ();
5             use CGI::Simple;
6              
7             our $VERSION = '0.22';
8             our $AUTHORITY = 'cpan:STEVAN';
9              
10             extends 'FCGI::Engine::Core';
11              
12             has 'handler_class' => (
13             metaclass => 'NoGetopt',
14             is => 'ro',
15             isa => 'Str | Object',
16             required => 1,
17             );
18              
19             has 'handler_method' => (
20             metaclass => 'NoGetopt',
21             is => 'ro',
22             isa => 'Str',
23             default => sub { 'handler' },
24             );
25              
26             has 'handler_args_builder' => (
27             metaclass => 'NoGetopt',
28             is => 'ro',
29             isa => 'CodeRef',
30             default => sub {
31             sub { CGI::Simple->new }
32             },
33             );
34              
35             augment 'initialize' => sub {
36             my ( $self, %addtional_options ) = @_;
37              
38             my $handler_class = $self->handler_class;
39             my $handler_method = $self->handler_method;
40             my $handler_args = $self->handler_args_builder;
41              
42             Class::Load::load_class($handler_class) unless blessed $handler_class;
43              
44             ($self->handler_class->can($handler_method))
45             || confess "The handler class ("
46             . $handler_class
47             . ") does not support the handler method ("
48             . $handler_method
49             . ")";
50             };
51              
52             sub create_environment { \%ENV }
53              
54             sub handle_request {
55             my $self = shift;
56             my $method = $self->handler_method;
57             $self->handler_class->$method( $self->handler_args_builder->() );
58             }
59              
60             1;
61              
62             __END__
63              
64             =pod
65              
66             =head1 NAME
67              
68             FCGI::Engine - A flexible engine for running FCGI-based applications
69              
70             =head1 SYNOPSIS
71              
72             # in scripts/my_web_app_fcgi.pl
73             use strict;
74             use warnings;
75              
76             use FCGI::Engine;
77              
78             FCGI::Engine->new_with_options(
79             handler_class => 'My::Web::Application',
80             handler_method => 'run',
81             pre_fork_init => sub {
82             require('my_web_app_startup.pl');
83             }
84             )->run;
85              
86             # run as normal FCGI script
87             perl scripts/my_web_app_fcgi.pl
88              
89             # run as standalone FCGI server
90             perl scripts/my_web_app_fcgi.pl --nproc 10 --pidfile /tmp/my_app.pid \
91             --listen /tmp/my_app.socket --daemon
92              
93             # see also FCGI::Engine::Manager for managing
94             # multiple FastCGI backends under one script
95              
96             =head1 DESCRIPTION
97              
98             This module helps manage FCGI based web applications by providing a
99             wrapper which handles most of the low-level FCGI details for you. It
100             can run FCGI programs as simple scripts or as full standalone
101             socket based servers who are managed by L<FCGI::Engine::ProcManager>.
102              
103             The code is largely based (*cough* stolen *cough*) on the
104             L<Catalyst::Engine::FastCGI> module, and provides a command line
105             interface which is compatible with that module. But of course it
106             does not require L<Catalyst> or anything L<Catalyst> related. So
107             you can use this module with your L<CGI::Application>-based web
108             application or any other L<Random::Web::Framework>-based web app.
109              
110             =head2 Using with Catalyst, Plack or other web frameworks
111              
112             This module (FCGI::Engine) is B<not> a replacement for L<Catalyst::Engine::FastCGI>
113             but instead the L<FCGI::Engine::Manager> (and all it's configuration tools) can be
114             used to manager L<Catalyst> apps as well as FCGI::Engine based applications. For
115             example, at work we have an application which has 6 different FCGI backends running.
116             Three of them use an ancient in-house web framework with simple FCGI::Engine wrappers,
117             one which uses L<CGI::Application> and an FCGI::Engine wrapper and then two L<Catalyst>
118             applications. They all happily and peacefully coexist and are all managed with the
119             same L<FCGI::Engine::Manager> script.
120              
121             As of version 0.11 we now have L<Plack>/L<PSGI> applications support via the
122             L<FCGI::Engine::Manager::Server::Plackup> module. See that module for more
123             information about how it can be used.
124              
125             =head2 Note about CGI.pm usage
126              
127             This module uses L<CGI::Simple> as a sane replacement for CGI.pm, it will pass in
128             a L<CGI::Simple> instance to your chosen C<handler_method> for you, so there is no
129             need to create your own instance of it. There have been a few cases from users who
130             have had bad interactions with CGI.pm and the instance of L<CGI::Simple> we create
131             for you, so before you spend hours looking for bugs in your app, check for this
132             first instead.
133              
134             If you want to change this behavior and not use L<CGI::Simple> then you can
135             override this using the C<handler_args_builder> option, see the docs on that
136             below for more details.
137              
138             =head1 CAVEAT
139              
140             This module is *NIX B<only>, it definitely does not work on Windows
141             and I have no intention of making it do so. Sorry.
142              
143             =head1 PARAMETERS
144              
145             =head2 Command Line
146              
147             This module uses L<MooseX::Getopt> for command line parameter
148             handling and validation.
149              
150             All parameters are currently optional, but some parameters
151             depend on one another.
152              
153             =over 4
154              
155             =item I<--listen -l>
156              
157             This should be a file path where the unix domain socket file
158             should live. If this parameter is specified, then you B<must>
159             also specify a location for the pidfile.
160              
161             =item I<--nproc -n>
162              
163             This should be an integer specifying the number of FCGI processes
164             that L<FCGI::Engine::ProcManager> should start up. The default is 1.
165              
166             =item I<--pidfile -p>
167              
168             This should be a file path where your pidfile should live. This
169             parameter is only used if the I<listen> parameter is specified.
170              
171             =item I<--daemon -d>
172              
173             This is a boolean parameter and has no argument, it is either
174             used or not. It determines if the script should daemonize itself.
175             This parameter only used if the I<listen> parameter is specified.
176              
177             =item I<--manager -m>
178              
179             This allows you to pass the name of a L<FCGI::ProcManager> subclass
180             to use. The default is to use L<FCGI::Engine::ProcManager>, and any value
181             passed to this parameter B<must> be a subclass of L<FCGI::ProcManager>.
182              
183             =back
184              
185             =head2 Constructor
186              
187             In addition to the command line parameters, there are a couple
188             parameters that the constuctor expects.
189              
190             =over 4
191              
192             =item I<handler_class>
193              
194             This is expected to be a class name, which will be used inside
195             the request loop to dispatch your web application.
196              
197             =item I<handler_method>
198              
199             This is the class method to be called on the I<handler_class>
200             to server as a dispatch entry point to your web application. It
201             will default to C<handler>.
202              
203             =item I<handler_args_builder>
204              
205             This must be a CODE ref that when called produces the arguments
206             to pass to the I<handler_method>. It defaults to a sub which
207             returns a L<CGI::Simple> object.
208              
209             =item I<pre_fork_init>
210              
211             This is an optional CODE reference which will be executed prior
212             to the request loop, and in a multi-proc context, prior to any
213             forking (so as to take advantage of OS COW features).
214              
215             =back
216              
217             =head1 METHODS
218              
219             =head2 Command Line Related
220              
221             =over 4
222              
223             =item B<listen>
224              
225             Returns the value passed on the command line with I<--listen>.
226             This will return a L<Path::Class::File> object.
227              
228             =item B<is_listening>
229              
230             A predicate used to determine if the I<--listen> parameter was
231             specified.
232              
233             =item B<nproc>
234              
235             Returns the value passed on the command line with I<--nproc>.
236              
237             =item B<pidfile>
238              
239             Returns the value passed on the command line with I<--pidfile>.
240             This will return a L<Path::Class::File> object.
241              
242             =item B<has_pidfile>
243              
244             A predicate used to determine if the I<--pidfile> parameter was
245             specified.
246              
247             =item B<detach>
248              
249             Returns the value passed on the command line with I<--daemon>.
250              
251             =item B<should_detach>
252              
253             A predicate used to determine if the I<--daemon> parameter was
254             specified.
255              
256             =item B<manager>
257              
258             Returns the value passed on the command line with I<--manager>.
259              
260             =back
261              
262             =head2 Inspection
263              
264             =over 4
265              
266             =item B<has_pre_fork_init>
267              
268             A predicate telling you if anything was passed to the
269             I<pre_fork_init> constructor parameter.
270              
271             =back
272              
273             =head2 Important Stuff
274              
275             =over 4
276              
277             =item B<run (%addtional_options)>
278              
279             Call this to start the show.
280              
281             It passes the C<%addtional_options> arguments to both the
282             C<pre_fork_init> sub and as constructor args to the
283             C<proc_manager>.
284              
285             =back
286              
287             =head2 Other Stuff
288              
289             =over 4
290              
291             =item B<BUILD>
292              
293             This is the L<Moose> BUILD method, it checks some of
294             our parameters to be sure all is sane.
295              
296             =item B<meta>
297              
298             This returns the L<Moose> metaclass assocaited with
299             this class.
300              
301             =back
302              
303             =head1 SEE ALSO
304              
305             =over 4
306              
307             =item L<Catalyst::Engine::FastCGI>
308              
309             I took all the guts of that module and squished them around a bit and
310             stuffed them in here.
311              
312             =item L<MooseX::Getopt>
313              
314             =item L<FCGI::ProcManager>
315              
316             I refactored this module and renamed it L<FCGI::Engine::ProcManager>,
317             which is now included in this distro.
318              
319             =back
320              
321             =head1 BUGS
322              
323             All complex software has bugs lurking in it, and this module is no
324             exception. If you find a bug please either email me, or add the bug
325             to cpan-RT.
326              
327             =head1 AUTHOR
328              
329             Stevan Little E<lt>stevan@iinteractive.comE<gt>
330              
331             Contributions from:
332              
333             Marcus Ramberg
334              
335             Bradley C. Bailey
336              
337             Brian Cassidy
338              
339             Johannes Plunien
340              
341             =head1 COPYRIGHT AND LICENSE
342              
343             Copyright 2007-2010 by Infinity Interactive, Inc.
344              
345             L<http://www.iinteractive.com>
346              
347             This library is free software; you can redistribute it and/or modify
348             it under the same terms as Perl itself.
349              
350             =cut
351              
352