| 1 | package PAGI::App::Loader; | ||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::App::Loader::VERSION = '0.002002'; | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | PAGI::App::Loader - Load PAGI app from file | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | use PAGI::App::Loader; | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | my $app = PAGI::App::Loader->new( | ||||||||||||||||||||||||||||
| 16 | file => 'app.pl', | ||||||||||||||||||||||||||||
| 17 | )->to_app; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 22 | 2 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | return bless { | ||||||||||||||||||||||||||||
| 25 | file => $args{file}, | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | reload => $args{reload} // 0, | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"reload"} // 0
MC/DC0%0/1
$args{"reload"} // 0
$args{"reload"}
Truth table50%1/2
A
A= $args{"reload"}
| |||||||||||||||||||||||||||||
| 27 | _app => undef, | ||||||||||||||||||||||||||||
| 28 | _mtime => 0, | ||||||||||||||||||||||||||||
| 29 | }, $class; | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | sub to_app { | ||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 36 | 2 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 37 | 2 | my $app = $self->_get_app(); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | unless ($app) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 1 | await $send->({ | |||||||||||||||||||||||||||
| 41 | type => 'http.response.start', | ||||||||||||||||||||||||||||
| 42 | status => 500, | ||||||||||||||||||||||||||||
| 43 | headers => [['content-type', 'text/plain']], | ||||||||||||||||||||||||||||
| 44 | }); | ||||||||||||||||||||||||||||
| 45 | 1 | await $send->({ type => 'http.response.body', body => 'App failed to load', more => 0 }); | |||||||||||||||||||||||||||
| 46 | 1 | return; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 50 | 2 | }; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | sub _get_app { | ||||||||||||||||||||||||||
|
Subroutine
_get_app: called CC=12 SCAR=31.7
| |||||||||||||||||||||||||||||
| 54 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | my $file = $self->{file}; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # Check if reload needed | ||||||||||||||||||||||||||||
| 59 | 2 | ▶ | if ($self->{reload} && $self->{_app}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"reload"} and $self->{"_app"}
MC/DC0%0/2
$self->{"reload"} and $self->{"_app"}
$self->{"reload"}
$self->{"_app"}
Truth table33%1/3
A and B
A= $self->{"reload"}B= $self->{"_app"}
| |||||||||||||||||||||||||||||
| 60 | 0 | my @stat = stat($file); | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | if (@stat && $stat[9] > $self->{_mtime}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@stat and $stat[9] > $self->{"_mtime"}
MC/DC0%0/2
@stat and $stat[9] > $self->{"_mtime"}
@stat
$stat[9] > $self->{"_mtime"}
Truth table0%0/3
A and B
A= @statB= $stat[9] > $self->{"_mtime"}
| |||||||||||||||||||||||||||||
| 62 | 0 | $self->{_app} = undef; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | ▶ | return $self->{_app} if $self->{_app}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Load app | ||||||||||||||||||||||||||||
| 69 | 2 | my @stat = stat($file); | |||||||||||||||||||||||||||
| 70 | 2 | ▶ | $self->{_mtime} = $stat[9] if @stat; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2 | my $app = do $file; | |||||||||||||||||||||||||||
| 73 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | warn "Error loading $file: $@\n"; | |||||||||||||||||||||||||||
| 75 | 0 | return; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 2 | ▶ | unless ($app) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 1 | ▶ | warn "Error loading $file: $!\n" if $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 1 | ▶ | warn "$file did not return a coderef\n" unless $app; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | return; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 1 | ▶ | unless (ref $app eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | warn "$file did not return a coderef\n"; | |||||||||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | $self->{_app} = $app; | |||||||||||||||||||||||||||
| 88 | 1 | return $app; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | 1; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | __END__ | ||||||||||||||||||||||||||||