| 1 | 1 | ▶ | use v5.40; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 1 | ▶ | use experimental 'signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use PAGI::Request; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use PAGI::Stash; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use PAGI::Nano; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | # Demonstrates that a mounted sub-app sees the request stash set by the parent's | ||||||||||||
| 9 | # middleware AND the lifecycle objects the parent put in app state at startup. | ||||||||||||
| 10 | # | ||||||||||||
| 11 | # Both ride the PAGI scope: the stash lives at $scope->{'pagi.stash'} and the | ||||||||||||
| 12 | # shared state at $scope->{state}; mount passes the scope through, so the mounted | ||||||||||||
| 13 | # app reads the very same objects. | ||||||||||||
| 14 | # | ||||||||||||
| 15 | # pagi-server app.pl | ||||||||||||
| 16 | # curl -H 'X-User: Ada' http://127.0.0.1:5000/api/hello | ||||||||||||
| 17 | |||||||||||||
| 18 | # A lifecycle object: built once at startup, then shared (and mutated) for the | ||||||||||||
| 19 | # app's lifetime. The greeting count proves the mounted app and the parent use | ||||||||||||
| 20 | # the same instance. | ||||||||||||
| 21 | 1 | package Greeter { | |||||||||||
| 22 | 1 | ▶ | use v5.40; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 1 | ▶ | use experimental 'signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 1 | ▶ | sub new ($class, %args) { bless { template => $args{template}, count => 0 }, $class } | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | 2 | ▶ | sub greet ($self, $name) { $self->{count}++; sprintf($self->{template}, $name) } | ||||||||||
|
Subroutine
greet: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 3 | ▶ | sub count ($self) { $self->{count} } | ||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | # Parent middleware: stash the current user (from a header) on each request. The | ||||||||||||
| 30 | # stash wraps $scope->{'pagi.stash'}, so whatever runs downstream — including the | ||||||||||||
| 31 | # mounted app — sees it. | ||||||||||||
| 32 | 4 | ▶ | my $stash_user = async sub ($scope, $receive, $send, $next) { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 33 | 4 | ▶ | my $user = PAGI::Request->new($scope)->header('x-user') // 'guest'; | ||||||||||
|
Condition100%2/2
"PAGI::Request"->new($scope)->header("x-user") // "guest"
MC/DC100%1/1
"PAGI::Request"->new($scope)->header("x-user") // "guest"
"PAGI::Request"->new($scope)->header("x-user")
Truth table100%2/2
A
A= "PAGI::Request"->new($scope)->header("x-user")
| |||||||||||||
| 34 | 4 | PAGI::Stash->new($scope)->set(user => $user); | |||||||||||
| 35 | 4 | await $next->(); | |||||||||||
| 36 | 1 | }; | |||||||||||
| 37 | |||||||||||||
| 38 | # The mounted sub-app. Its handlers never set up the greeter or the user; they | ||||||||||||
| 39 | # just read them off the context that flowed down through the mount. | ||||||||||||
| 40 | 1 | ▶ | my $api = app { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 41 | 2 | ▶ | get '/hello' => sub ($c) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 42 | 2 | my $user = $c->stash->get('user'); # set by parent middleware | |||||||||||
| 43 | 2 | my $greeter = $c->state->{greeter}; # built by parent startup | |||||||||||
| 44 | { | ||||||||||||
| 45 | 2 | from => 'mounted /api app', | |||||||||||
| 46 | user => $user, | ||||||||||||
| 47 | greeting => $greeter->greet($user), | ||||||||||||
| 48 | greetings_so_far => $greeter->count, | ||||||||||||
| 49 | }; | ||||||||||||
| 50 | 1 | }; | |||||||||||
| 51 | 1 | }; | |||||||||||
| 52 | |||||||||||||
| 53 | 1 | ▶ | my $app = app { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | 1 | ▶ | startup async sub ($state) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 55 | 1 | $state->{greeter} = Greeter->new(template => 'Hello, %s!'); | |||||||||||
| 56 | 1 | }; | |||||||||||
| 57 | |||||||||||||
| 58 | 1 | enable $stash_user; | |||||||||||
| 59 | |||||||||||||
| 60 | 0 | ▶ | get '/' => sub ($c) { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 61 | 0 | { hint => 'GET /api/hello with an X-User header' }; | |||||||||||
| 62 | 1 | }; | |||||||||||
| 63 | |||||||||||||
| 64 | # The parent can see the same lifecycle object the mount uses. | ||||||||||||
| 65 | 1 | ▶ | get '/greetings' => sub ($c) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 66 | 1 | { greetings_so_far => $c->state->{greeter}->count }; | |||||||||||
| 67 | 1 | }; | |||||||||||
| 68 | |||||||||||||
| 69 | 1 | mount '/api' => $api; | |||||||||||
| 70 | 1 | }; | |||||||||||
| 71 | |||||||||||||
| 72 | 1 | $app; | |||||||||||