| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MyApp::API; |
|
2
|
1
|
|
|
1
|
|
18
|
use parent 'PAGI::Endpoint::Router'; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
7
|
|
|
3
|
1
|
|
|
1
|
|
72
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
19
|
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
36
|
|
|
5
|
1
|
|
|
1
|
|
4
|
use Future::AsyncAwait; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
5
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
my @USERS = ( |
|
8
|
|
|
|
|
|
|
{ id => 1, name => 'Alice', email => 'alice@example.com' }, |
|
9
|
|
|
|
|
|
|
{ id => 2, name => 'Bob', email => 'bob@example.com' }, |
|
10
|
|
|
|
|
|
|
); |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub routes { |
|
13
|
1
|
|
|
1
|
1
|
2
|
my ($self, $r) = @_; |
|
14
|
|
|
|
|
|
|
|
|
15
|
1
|
|
|
|
|
4
|
$r->get('/info' => 'get_info'); |
|
16
|
1
|
|
|
|
|
2
|
$r->get('/users' => 'list_users'); |
|
17
|
1
|
|
|
|
|
3
|
$r->get('/users/:id' => 'get_user'); |
|
18
|
1
|
|
|
|
|
3
|
$r->post('/users' => 'create_user'); |
|
19
|
|
|
|
|
|
|
} |
|
20
|
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
0
|
3
|
async sub get_info { |
|
22
|
1
|
|
|
|
|
3
|
my ($self, $req, $res) = @_; |
|
23
|
1
|
|
|
|
|
5
|
$req->state->{metrics}{requests}++; |
|
24
|
|
|
|
|
|
|
|
|
25
|
1
|
|
|
|
|
2
|
my $config = $req->state->{config}; |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
await $res->json({ |
|
28
|
|
|
|
|
|
|
app => $config->{app_name}, |
|
29
|
|
|
|
|
|
|
version => $config->{version}, |
|
30
|
1
|
|
|
|
|
8
|
api => 'v1', |
|
31
|
|
|
|
|
|
|
}); |
|
32
|
|
|
|
|
|
|
} |
|
33
|
|
|
|
|
|
|
|
|
34
|
1
|
|
|
1
|
0
|
2
|
async sub list_users { |
|
35
|
1
|
|
|
|
|
3
|
my ($self, $req, $res) = @_; |
|
36
|
1
|
|
|
|
|
5
|
$req->state->{metrics}{requests}++; |
|
37
|
1
|
|
|
|
|
4
|
await $res->json(\@USERS); |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
0
|
|
|
0
|
0
|
|
async sub get_user { |
|
41
|
0
|
|
|
|
|
|
my ($self, $req, $res) = @_; |
|
42
|
0
|
|
|
|
|
|
$req->state->{metrics}{requests}++; |
|
43
|
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
my $id = $req->path_param('id'); |
|
45
|
0
|
|
|
|
|
|
my ($user) = grep { $_->{id} == $id } @USERS; |
|
|
0
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
|
if ($user) { |
|
48
|
0
|
|
|
|
|
|
await $res->json($user); |
|
49
|
|
|
|
|
|
|
} else { |
|
50
|
0
|
|
|
|
|
|
await $res->status(404)->json({ error => 'User not found' }); |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
0
|
|
|
0
|
0
|
|
async sub create_user { |
|
55
|
0
|
|
|
|
|
|
my ($self, $req, $res) = @_; |
|
56
|
0
|
|
|
|
|
|
$req->state->{metrics}{requests}++; |
|
57
|
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my $data = await $req->json; |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $new_user = { |
|
61
|
|
|
|
|
|
|
id => scalar(@USERS) + 1, |
|
62
|
|
|
|
|
|
|
name => $data->{name}, |
|
63
|
|
|
|
|
|
|
email => $data->{email}, |
|
64
|
0
|
|
|
|
|
|
}; |
|
65
|
0
|
|
|
|
|
|
push @USERS, $new_user; |
|
66
|
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
await $res->status(201)->json($new_user); |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |