| 1 | package PAGI::App::Healthcheck; | ||||||||||||
| 2 | 1 | $PAGI::App::Healthcheck::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 JSON::MaybeXS (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | =head1 NAME | ||||||||||||
| 9 | |||||||||||||
| 10 | PAGI::App::Healthcheck - Health check endpoint app | ||||||||||||
| 11 | |||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||
| 13 | |||||||||||||
| 14 | use PAGI::App::Healthcheck; | ||||||||||||
| 15 | |||||||||||||
| 16 | my $app = PAGI::App::Healthcheck->new( | ||||||||||||
| 17 | checks => { | ||||||||||||
| 18 | database => sub { check_db() }, | ||||||||||||
| 19 | }, | ||||||||||||
| 20 | )->to_app; | ||||||||||||
| 21 | |||||||||||||
| 22 | =cut | ||||||||||||
| 23 | |||||||||||||
| 24 | 1 | our $START_TIME = time(); | |||||||||||
| 25 | |||||||||||||
| 26 | 5 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 27 | 5 | my ($class, %args) = @_; | |||||||||||
| 28 | |||||||||||||
| 29 | return bless { | ||||||||||||
| 30 | checks => $args{checks} // {}, | ||||||||||||
| 31 | version => $args{version}, | ||||||||||||
| 32 | 5 | ▶ | }, $class; | ||||||||||
|
Condition100%2/2
$args{"checks"} // {}
MC/DC100%1/1
$args{"checks"} // {}
$args{"checks"}
Truth table100%2/2
A
A= $args{"checks"}
| |||||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 5 | ▶ | sub to_app { | ||||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 36 | 5 | my ($self) = @_; | |||||||||||
| 37 | |||||||||||||
| 38 | 5 | my $checks = $self->{checks}; | |||||||||||
| 39 | 5 | my $version = $self->{version}; | |||||||||||
| 40 | |||||||||||||
| 41 | 5 | ▶ | return async sub { | ||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||
| 42 | 5 | my ($scope, $receive, $send) = @_; | |||||||||||
| 43 | 5 | my $all_ok = 1; | |||||||||||
| 44 | 5 | my %results; | |||||||||||
| 45 | |||||||||||||
| 46 | 5 | for my $name (sort keys %$checks) { | |||||||||||
| 47 | 4 | my $check = $checks->{$name}; | |||||||||||
| 48 | 4 | my $result = { status => 'ok' }; | |||||||||||
| 49 | |||||||||||||
| 50 | 4 | ▶ | eval { my $ok = $check->(); $result->{status} = 'error' unless $ok; }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | 4 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | 1 | $result->{status} = 'error'; | |||||||||||
| 53 | 1 | $result->{message} = "$@"; | |||||||||||
| 54 | 1 | $result->{message} =~ s/\s+$//; | |||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | 4 | $results{$name} = $result; | |||||||||||
| 58 | 4 | ▶ | $all_ok = 0 if $result->{status} eq 'error'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 5 | ▶ | my $response = { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | status => $all_ok ? 'ok' : 'error', | ||||||||||||
| 63 | timestamp => time(), | ||||||||||||
| 64 | uptime => time() - $START_TIME, | ||||||||||||
| 65 | }; | ||||||||||||
| 66 | 5 | ▶ | $response->{version} = $version if defined $version; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 5 | ▶ | $response->{checks} = \%results if %results; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | |||||||||||||
| 69 | 5 | my $body = JSON::MaybeXS::encode_json($response); | |||||||||||
| 70 | 5 | ▶ | my $status = $all_ok ? 200 : 503; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | |||||||||||||
| 72 | 5 | await $send->({ | |||||||||||
| 73 | type => 'http.response.start', | ||||||||||||
| 74 | status => $status, | ||||||||||||
| 75 | headers => [ | ||||||||||||
| 76 | ['content-type', 'application/json'], | ||||||||||||
| 77 | ['content-length', length($body)], | ||||||||||||
| 78 | ['cache-control', 'no-cache'], | ||||||||||||
| 79 | ], | ||||||||||||
| 80 | }); | ||||||||||||
| 81 | 5 | await $send->({ type => 'http.response.body', body => $body, more => 0 }); | |||||||||||
| 82 | 5 | }; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 1 | 1; | |||||||||||
| 86 | |||||||||||||
| 87 | __END__ | ||||||||||||