File Coverage

blib/lib/Mojolicious/Plugin/MountPSGI.pm
Criterion Covered Total %
statement 22 29 75.8
branch 7 16 43.7
condition n/a
subroutine 3 3 100.0
pod 1 1 100.0
total 33 49 67.3


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::MountPSGI;
2 6     6   17098 use Mojo::Base 'Mojolicious::Plugin';
  6         11  
  6         41  
3 6     6   3378 use Mojolicious::Plugin::MountPSGI::Proxy;
  6         16  
  6         2251  
4              
5              
6             our $VERSION = '0.13';
7              
8             sub register {
9 8     8 1 27856 my ($self, $app, $conf) = @_;
10              
11 8         22 my $rewrite = delete $conf->{rewrite};
12              
13             # Extract host and path
14 8         29 my $prefix = (keys %$conf)[0];
15 8         16 my ($host, $path);
16 8 50       52 if ($prefix =~ /^(\*\.)?([^\/]+)(\/.*)?$/) {
17 0         0 $host = quotemeta $2;
18 0 0       0 $host = "(?:.*\\.)?$host" if $1;
19 0         0 $path = $3;
20 0 0       0 $path = '/' unless defined $path;
21 0         0 $host = qr/^$host$/i;
22             }
23 8         19 else { $path = $prefix }
24              
25 8 100       20 $rewrite = $rewrite ? $path : undef;
26              
27 8         13 my $proxy;
28 8         21 my $psgi = $conf->{$prefix};
29 8 100       21 if (ref $psgi) {
30 1         10 $proxy = Mojolicious::Plugin::MountPSGI::Proxy->new(app => $psgi, rewrite => $rewrite);
31             } else {
32 7 50       181 unless (-r $psgi) {
33 0         0 my $abs = $app->home->rel_file($psgi);
34 0 0       0 $psgi = $abs if -r $abs;
35             }
36 7         81 $proxy = Mojolicious::Plugin::MountPSGI::Proxy->new(script => $psgi, rewrite => $rewrite);
37             }
38              
39             # Generate route
40 8         13139 my $route = $app->routes->route($path)->detour(app => $proxy);
41 8 50       2036 $route->over(host => $host) if $host;
42              
43 8         43 return $route;
44             }
45              
46              
47             1;
48             __END__