examples/chat-showcase/app.pl

filter: Statementstmt 89.8% 53 / 59 Branchbran 75.0% 6 / 8 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 91.6% 11 / 12 Podpod n/a totaltotal 79.5% 74 / 93 CC 10 SCAR 12.7
CC
10
Cov
83%
__ANON__
7.4
__ANON__
3.0
__ANON__
2.1
CRAP 3.6
« examples/job-runner/app.pl ↑ Summary examples/tasks-modulino/lib/MyApp.pm »
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::Nano;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Ports PAGI-Tools' 10-chat-showcase / websocket-chat-v2 to PAGI::Nano.
7 # A multi-user chat exercising every protocol at once: a REST rooms API (HTTP),
8 # room messaging with broadcast (WebSocket), a presence feed (SSE), app-lifetime
9 # room state (lifespan), and a custom request logger (event-layer middleware).
10 #
11 # pagi-server app.pl
12 # # connect a WS client to ws://127.0.0.1:5000/ws/chat and send
13 # # {"join":"general","user":"ada"} then {"text":"hello"}
14  
15 # Coderef request logger. By convention a coderef middleware calls $next->().
16 4 ▶my $logger = async sub ($scope, $receive, $send, $next) {
Subroutine __ANON__: called CC=3 SCAR=11.0
17 4 ▶ warn "[req] $scope->{method} $scope->{path}\n" if ($scope->{type} // '') eq 'http';
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} // "") eq "http" 2 2 4
Condition50%1/2
$scope->{"type"} // ""
l!l
40
MC/DC0%0/1
$scope->{"type"} // ""
$scope->{"type"}
Truth table50%1/2
A
A= $scope->{"type"}
Aresult
11
00
18 4 await $next->();
19 1 };
20  
21 1 ▶my $app = app {
Subroutine __ANON__: called CC=1 SCAR=0.1
22 1 ▶ startup async sub ($state) {
Subroutine __ANON__: called CC=1 SCAR=0.0
23 1 $state->{rooms} = { general => { messages => [], members => {} } };
24 1 $state->{presence} = []; # SSE subscribers (PAGI::SSE handles)
25 1 };
26  
27 1 enable $logger;
28  
29 # --- REST rooms API ---
30 1 ▶ get '/api/rooms' => sub ($c) { [ sort keys %{ $c->state->{rooms} } ] };
Subroutine __ANON__: called CC=1 SCAR=0.0
31  
32 1 ▶ get '/api/room/:name/history' => sub ($c, $name) {
Subroutine __ANON__: called CC=2 SCAR=7.2
33 1 ▶ my $room = $c->state->{rooms}{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $room = $c->state->{"rooms"}{$name} or return $c->json({"error", "no such room"}, "status", 404) 1 0 1
34 or return $c->json({ error => 'no such room' }, status => 404);
35 1 $room->{messages};
36 1 };
37  
38 # --- WebSocket chat: join a room, then broadcast messages to its members ---
39 1 ▶ websocket '/ws/chat' => async sub ($c) {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 1 my $ws = $c->websocket;
41 1 my $rooms = $c->state->{rooms};
42 1 await $ws->accept;
43  
44 1 my ($room_name, $user, $room);
45 2 ▶ await $ws->each_json(async sub ($msg) {
Subroutine __ANON__: called CC=7 SCAR=20.0
46 2 ▶ if (my $r = $msg->{join}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $r = $msg->{'join'}) { } 1 1 2
elsif (defined $msg->{'text'} and $room) { } 1 0 1
47 1 $room_name = $r;
48 1 ▶ $user = $msg->{user} // 'anon';
Condition50%1/2
$msg->{"user"} // "anon"
l!l
10
MC/DC0%0/1
$msg->{"user"} // "anon"
$msg->{"user"}
Truth table50%1/2
A
A= $msg->{"user"}
Aresult
11
00
49 1 ▶ $room = $rooms->{$room_name} //= { messages => [], members => {} };
Condition50%1/2
$rooms->{$room_name} //= {"messages", [], "members", {}}
l!l
10
MC/DC0%0/1
$rooms->{$room_name} //= {"messages", [], "members", {}}
$rooms->{$room_name}
Truth table50%1/2
A
A= $rooms->{$room_name}
Aresult
11
00
50 1 $room->{members}{$user} = $ws;
51 1 await $ws->send_json({ system => "joined $room_name as $user" });
52 }
53 ▶ elsif (defined $msg->{text} && $room) {
Condition33%1/3
defined $msg->{"text"} and $room
!ll&&!rl&&r
001
MC/DC0%0/2
defined $msg->{"text"} and $room
defined $msg->{"text"} $room
Truth table33%1/3
A and B
A= defined $msg->{"text"}B= $room
ABresult
0X0
100
111
54 1 my $entry = { room => $room_name, user => $user, text => $msg->{text} };
55 1 push @{ $room->{messages} }, $entry;
56 1 for my $member (values %{ $room->{members} }) {
57 1 await $member->send_json_if_connected($entry); # broadcast
58 }
59 }
60 1 });
61 1 };
62  
63 # --- SSE presence feed ---
64 0 ▶ sse '/events' => async sub ($c) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 my $s = $c->sse;
66 0 await $s->send_event(event => 'rooms', data => join(',', sort keys %{ $c->state->{rooms} }));
67 0 await $s->close;
68 1 };
69 1 };
70  
71 1 $app;
« examples/job-runner/app.pl ↑ Summary examples/tasks-modulino/lib/MyApp.pm »