| 1 | package PAGI::App::Cascade; | ||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::App::Cascade::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 | 1 | ▶ | use PAGI::Utils (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | PAGI::App::Cascade - Try apps in sequence until success | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use PAGI::App::Cascade; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | my $app = PAGI::App::Cascade->new( | ||||||||||||||||||||||||||||
| 17 | apps => [$static_app, PAGI::App::NotFound->new(body => 'nope')], | ||||||||||||||||||||||||||||
| 18 | catch => [404, 405], | ||||||||||||||||||||||||||||
| 19 | )->to_app; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 4 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | return bless { | ||||||||||||||||||||||||||||
| 27 | 8 | ▶ | apps => [map { PAGI::Utils::to_app($_) } @{$args{apps} // []}], | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"apps"} // []
MC/DC0%0/1
$args{"apps"} // []
$args{"apps"}
Truth table50%1/2
A
A= $args{"apps"}
| |||||||||||||||||||||||||||||
| 28 | 4 | ▶ | catch => { map { $_ => 1 } @{$args{catch} // [404, 405]} }, | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"catch"} // [404, 405]
MC/DC100%1/1
$args{"catch"} // [404, 405]
$args{"catch"}
Truth table100%2/2
A
A= $args{"catch"}
| |||||||||||||||||||||||||||||
| 29 | }, $class; | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 1 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | push @{$self->{apps}}, PAGI::Utils::to_app($app); | |||||||||||||||||||||||||||
| 36 | 1 | return $self; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 4 | ▶ | sub to_app { | ||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 4 | my @apps = @{$self->{apps}}; | |||||||||||||||||||||||||||
| 43 | 4 | my %catch = %{$self->{catch}}; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 4 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 46 | 4 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 47 | 4 | for my $i (0 .. $#apps) { | |||||||||||||||||||||||||||
| 48 | 8 | my $app = $apps[$i]; | |||||||||||||||||||||||||||
| 49 | 8 | my $is_last = ($i == $#apps); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # For non-last apps, we need to capture the response | ||||||||||||||||||||||||||||
| 52 | 8 | ▶ | if (!$is_last) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 5 | my @captured_events; | |||||||||||||||||||||||||||
| 54 | my $captured_status; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 10 | ▶ | my $capture_send = async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 57 | 10 | my ($event) = @_; | |||||||||||||||||||||||||||
| 58 | 10 | push @captured_events, $event; | |||||||||||||||||||||||||||
| 59 | 10 | ▶ | if ($event->{type} eq 'http.response.start') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 5 | $captured_status = $event->{status}; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 5 | }; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 5 | await $app->($scope, $receive, $capture_send); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # Check if we should try next app | ||||||||||||||||||||||||||||
| 67 | 5 | ▶ | if ($captured_status && $catch{$captured_status}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$captured_status and $catch{$captured_status}
MC/DC50%1/2
$captured_status and $catch{$captured_status}
$captured_status
$catch{$captured_status}
Truth table66%2/3
A and B
A= $captured_statusB= $catch{$captured_status}
| |||||||||||||||||||||||||||||
| 68 | 4 | next; # Try next app | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Send captured events | ||||||||||||||||||||||||||||
| 72 | 1 | for my $event (@captured_events) { | |||||||||||||||||||||||||||
| 73 | 2 | await $send->($event); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 1 | return; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Last app - send directly | ||||||||||||||||||||||||||||
| 79 | 3 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 80 | 3 | return; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 4 | }; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | 1; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | __END__ | ||||||||||||||||||||||||||||