Plack/Handler/Net/Async/FastCGI.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 66 CC 3 SCAR 16.7
CC
3
Cov
0%
run
12.0
new
2.0
BEGIN
2.0
CRAP 5.3
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2010-2024 -- leonerd@leonerd.org.uk
5  
6 package Plack::Handler::Net::Async::FastCGI 0.26;
7  
8 use v5.14;
9 use warnings;
10  
11 use Net::Async::FastCGI::PSGI;
12 use IO::Async::Loop;
13  
14 =head1 NAME
15  
16 C<Plack::Handler::Net::Async::FastCGI> - FastCGI handler for Plack using L<Net::Async::FastCGI>
17  
18 =head1 DESCRIPTION
19  
20 This module allows L<Plack> to run a L<PSGI> application as a standalone
21 FastCGI daemon under L<IO::Async>, by using L<Net::Async::FastCGI>.
22  
23 plackup -s Net::Async::FastCGI --listen ":2000" application.psgi
24  
25 This is internally implemented using L<Net::Async::FastCGI::PSGI>; further
26 information on environment etc.. is documented there.
27  
28 =cut
29  
30 sub new
31 {
32 my $class = shift;
33 my %opts = @_;
34  
35 delete $opts{host};
36 delete $opts{port};
37  
38 my $self = bless {
39 map { $_ => delete $opts{$_} } qw( listen server_ready socket ),
40 }, $class;
41  
42 keys %opts and die "Unrecognised keys " . join( ", ", sort keys %opts );
43  
44 return $self;
45 }
46  
47 sub run
48 {
49 my $self = shift;
50 my ( $app ) = @_;
51  
52 my $loop = IO::Async::Loop->new;
53  
54 foreach my $listen ( @{ $self->{listen} } ) {
55 my $fcgi = Net::Async::FastCGI::PSGI->new(
56 app => $app,
57 );
58  
59 $loop->add( $fcgi );
60  
61 if( $self->{socket} ) {
62 my $path = $self->{socket};
63  
64 require IO::Socket::UNIX;
65  
66 unlink $path if -e $path;
67  
68 my $socket = IO::Socket::UNIX->new(
69 Local => $path,
70 Listen => 10,
71 ) or die "Cannot listen on $path - $!";
72  
73 $fcgi->configure( handle => $socket );
74 }
75 else {
76 my ( $host, $service ) = $listen =~ m/^(.*):(.*?)$/;
77  
78 $fcgi->listen(
79 host => $host,
80 service => $service,
81  
82 on_notifier => sub { $self->{server_ready} and $self->{server_ready}->() },
83  
84 on_resolve_error => sub {
85 die "Cannot resolve - $_[-1]\n";
86 },
87 on_listen_error => sub {
88 die "Cannot listen - $_[-1]\n";
89 },
90 );
91 }
92  
93 }
94  
95 $loop->loop_forever;
96 }
97  
98 =head1 SEE ALSO
99  
100 =over 4
101  
102 =item *
103  
104 L<Net::Async::FastCGI> - use FastCGI with L<IO::Async>
105  
106 =item *
107  
108 L<Plack> - Perl Superglue for Web frameworks and Web Servers (PSGI toolkit)
109  
110 =back
111  
112 =head1 AUTHOR
113  
114 Paul Evans <leonerd@leonerd.org.uk>
115  
116 =cut
117  
118 0x55AA;