Skip to content

Commit

Permalink
add healthcheck end point
Browse files Browse the repository at this point in the history
Add a /healthcheck end point, returning a simple JSON structure. Use a
template rather than normal JSON encoding to ensure that the template
engine is working correctly.

In the future, this could be extended to check that the API is
available, but for now a simple check that just the web server is
running is good enough.
  • Loading branch information
haarg committed Oct 10, 2024
1 parent ffbab6c commit bda4542
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/MetaCPAN/Web/Controller/Root.pm
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ sub robots : Path("robots.txt") : Args(0) {
} );
}

sub healthcheck : Local : Args(0) {
my ( $self, $c ) = @_;

$c->res->content_type('application/json');
$c->stash( {
template => 'healthcheck.tx',
status => 'healthy',
} );
}

=head2 end
Attempt to render a view, if needed.
Expand Down
1 change: 1 addition & 0 deletions root/healthcheck.tx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[% { status => $status }.json() | raw %]
19 changes: 19 additions & 0 deletions t/controller/healthcheck.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use strict;
use warnings;
use lib 't/lib';

use Cpanel::JSON::XS qw( decode_json );
use MetaCPAN::Web::Test qw( app GET test_psgi );
use Test::More;

test_psgi app, sub {
my $cb = shift;
ok( my $res = $cb->( GET '/healthcheck' ), 'GET /healthcheck' );
is( $res->code, 200, 'code 200' );
is $res->header('Content-Type'), 'application/json',
'correct Content-Type';
my $data = decode_json( $res->content );
is $data->{status}, 'healthy', 'has correct status';
};

done_testing;

0 comments on commit bda4542

Please sign in to comment.