File Coverage

blib/lib/WWW/CPAN/SQLite.pm
Criterion Covered Total %
statement 40 51 78.4
branch 5 10 50.0
condition n/a
subroutine 9 12 75.0
pod 0 3 0.0
total 54 76 71.0


line stmt bran cond sub pod time code
1             package WWW::CPAN::SQLite;
2              
3             # $Id: SQLite.pm 68 2019-01-04 00:15:58Z stro $
4              
5 3     3   113490 use strict;
  3         15  
  3         88  
6 3     3   48 use warnings;
  3         7  
  3         79  
7              
8 3     3   16 use File::Spec;
  3         5  
  3         84  
9 3     3   1353 use FindBin::Real;
  3         3520  
  3         134  
10 3     3   1267 use Plack::Middleware::Static;
  3         49642  
  3         94  
11 3     3   21 use Plack::MIME;
  3         5  
  3         55  
12 3     3   1308 use MIME::Types;
  3         12386  
  3         1644  
13              
14             Plack::MIME->add_type('.sqlite' => 'application/x-sqlite3');
15             Plack::MIME->set_fallback(sub { (MIME::Types::by_suffix $_[0])[0] });
16              
17             our $VERSION = 1.001;
18              
19             sub new {
20 2     2 0 1338 my $class = shift;
21              
22 2         8 my $static_dir = FindBin::Real::Bin();
23 2 50       339 if ($static_dir =~ m!bin$!) {
24 0         0 $static_dir = File::Spec->catfile(FindBin::Real::Bin(), '..');
25             }
26              
27 2         38 return bless {
28             'files_to_keep' => 10,
29             'ttl' => 180,
30             'last_check' => 0,
31             'next_check' => 0,
32             'static_dir' => File::Spec->catfile($static_dir, 'static'),
33             'last_file' => '',
34             }, $class;
35             }
36              
37             sub run {
38 0     0 0 0 my $self = shift;
39              
40 0     0   0 my $app = sub { $self->psgi(@_) };
  0         0  
41              
42             return Plack::Middleware::Static->wrap(
43             $app,
44 0     0   0 'path' => sub { s!^/static/!! },
45 0         0 'root' => 'static/',
46             );
47             }
48              
49             sub psgi {
50 2     2 0 24603 my $self = shift;
51 2         5 my ($env) = @_;
52              
53 2 50       7 if (time > $self->{'next_check'}) {
54 2 100       81 if (opendir my $DIR, $self->{'static_dir'}) {
55 1         31 my @files = sort { $b cmp $a } grep { m!\.sqlite$! } readdir $DIR;
  0         0  
  3         33  
56 1         17 closedir $DIR;
57 1         6 while (my $path = shift @files) {
58 1 50       69 if (-s File::Spec->catfile($self->{'static_dir'}, $path)) {
59 1         7 $self->{'last_file'} = $path;
60 1         3 $self->{'last_check'} = time;
61 1         3 $self->{'next_check'} = $self->{'last_check'} + $self->{'ttl'};
62 1         15 return [ 302, [ 'Location' => '/static/' . $path ], [ $path ] ];
63             }
64             }
65 0         0 return [ 404, [ 'Content-Type' => 'text/plain' ], [ 'No index files, build your own' ] ];
66             } else {
67 1         104 mkdir $self->{'static_dir'};
68 1         17 return [ 404, [ 'Content-Type' => 'text/plain' ], [ 'No index files, build your own' ] ];
69             }
70             } else {
71 0 0         if (my $path = $self->{'last_file'}) {
72 0           return [ 302, [ 'Location' => '/static/' . $path ], [ $path ] ];
73             } else {
74 0           return [ 404, [ 'Content-Type' => 'text/plain' ], [ 'No index files, build your own' ] ];
75             }
76             }
77             }
78              
79             1;
80              
81             =head1 NAME
82              
83             WWW::CPAN::SQLite - generate and provide precompiled CPAN::SQLite database
84              
85             =head1 VERSION
86              
87             version 1.001
88              
89             =head1 DESCRIPTION
90              
91             This package is used to generate and provide pre-compiled L database.
92              
93             If you use DarkPAN and multiple CPAN clients that shouldn't use too much
94             resources, or clients that have no access to the internet, you may want
95             to create your own database. Otherwise, there's little use for you as you
96             can use the existing database provided on cpansqlite.trouchelle.com.
97              
98             =head1 AUTHOR
99              
100             Serguei Trouchelle Estro@cpan.orgE
101              
102             =head1 COPYRIGHT
103              
104             Copyright 2019 by Serguei Trouchelle Estro@cpan.orgE.
105              
106             Use and redistribution are under the same terms as Perl itself.
107              
108             =cut