File Coverage

blib/lib/Mojolicious/Command/static.pm
Criterion Covered Total %
statement 12 36 33.3
branch 0 4 0.0
condition n/a
subroutine 4 11 36.3
pod 1 1 100.0
total 17 52 32.6


line stmt bran cond sub pod time code
1             package Mojolicious::Command::static;
2 1     1   705 use Mojo::Base 'Mojolicious::Command';
  1         1  
  1         10  
3              
4             # This command is a copy of Mojolicious::Command::daemon
5              
6 1     1   195562 use Getopt::Long qw(GetOptionsFromArray :config no_auto_abbrev no_ignore_case);
  1         11493  
  1         5  
7 1     1   1148 use Mojo::Server::Daemon;
  1         80193  
  1         12  
8              
9             has description => 'Quickly serve static files';
10             has usage => sub { shift->extract_usage };
11              
12 1     1   83 use File::Basename;
  1         2  
  1         611  
13              
14             sub run {
15 0     0 1   my ($self, @args) = @_;
16              
17 0           my $daemon = Mojo::Server::Daemon->new(app => Mojolicious->new);
18             GetOptionsFromArray \@args,
19 0     0     'b|backlog=i' => sub { $daemon->backlog($_[1]) },
20 0     0     'c|clients=i' => sub { $daemon->max_clients($_[1]) },
21 0     0     'i|inactivity-timeout=i' => sub { $daemon->inactivity_timeout($_[1]) },
22             'l|listen=s' => \my @listen,
23 0     0     'p|proxy' => sub { $daemon->reverse_proxy(1) },
24 0     0     'r|requests=i' => sub { $daemon->max_requests($_[1]) };
  0            
25              
26 0           push @{$daemon->app->renderer->classes}, __PACKAGE__;
  0            
27              
28             # Add all the paths and paths of filenames specified on the command line
29 0 0         $daemon->app->static->paths([grep { -d $_ } map { -f $_ ? dirname $_ : $_ } @args, '.']);
  0            
  0            
30              
31             # Create numeric shortcuts for each filename specified on the command line
32 0           my @files;
33 0           foreach my $file ( grep { -f $_ } @args ) {
  0            
34 0           push @files, basename $file;
35             $daemon->app->routes->get('/'.($#files+1))->to(cb=>sub {
36 0     0     my $c = shift;
37 0           $c->res->headers->content_disposition(sprintf "attachment; filename=%s;", basename $file);
38 0           $c->reply->asset(Mojo::Asset::File->new(path => $file));
39 0           });
40             }
41              
42             # Build an index of the available specified files
43 0           $daemon->app->routes->get('/')->name('index')->to(files => \@files);
44              
45 0 0         $daemon->listen(\@listen) if @listen;
46 0           $daemon->run;
47             }
48              
49             1;
50              
51             =encoding utf8
52              
53             =head1 NAME
54              
55             Mojolicious::Command::static - Quickly serve static files
56              
57             =head1 SYNOPSIS
58              
59             Usage: APPLICATION static [OPTIONS] dir1 dir2 ... file1 file2 ...
60              
61             ./myapp.pl static
62              
63             Options:
64             -h, --help Show this summary of available options
65              
66             =head1 DESCRIPTION
67              
68             L quickly serves static files
69              
70             Serves files from the current directory as well as those specified on the
71             command line. Numeric shortcuts (e.g. /1, /2, etc) are created for files
72             that are specified on the command line.
73              
74             =head1 ATTRIBUTES
75              
76             L inherits all attributes from
77             L and implements the following new ones.
78              
79             =head2 description
80              
81             my $description = $static->description;
82             $static = $static->description('Foo');
83              
84             Short description of this command, used for the command list.
85              
86             =head2 usage
87              
88             my $usage = $static->usage;
89             $routes = $static->usage('Foo');
90              
91             Usage information for this command, used for the help screen.
92              
93             =head1 METHODS
94              
95             L inherits all methods from
96             L and implements the following new ones.
97              
98             =head2 run
99              
100             $static->run(@ARGV);
101              
102             Run this command.
103              
104             =head1 SEE ALSO
105              
106             L, L, L.
107              
108             =cut
109              
110             __DATA__