Skip to content

Commit bb06ff0

Browse files
committed
add healthcheck end point
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.
1 parent ffbab6c commit bb06ff0

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

lib/MetaCPAN/Web/Controller/Root.pm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ sub robots : Path("robots.txt") : Args(0) {
9393
} );
9494
}
9595

96+
sub healthcheck : Local : Args(0) {
97+
my ( $self, $c ) = @_;
98+
99+
$c->res->content_type('application/json');
100+
$c->stash( {
101+
template => 'healthcheck.tx',
102+
status => 'healthy',
103+
} );
104+
}
105+
96106
=head2 end
97107
98108
Attempt to render a view, if needed.

root/healthcheck.tx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[% { status => $status }.json() | raw %]

t/controller/healthcheck.t

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use strict;
2+
use warnings;
3+
use lib 't/lib';
4+
5+
use Cpanel::JSON::XS qw(decode_json);
6+
use MetaCPAN::Web::Test qw( app GET test_psgi );
7+
use Test::More;
8+
9+
test_psgi app, sub {
10+
my $cb = shift;
11+
ok( my $res = $cb->( GET '/healthcheck' ), 'GET /healthcheck' );
12+
is( $res->code, 200, 'code 200' );
13+
is $res->header('Content-Type'), 'application/json', 'correct Content-Type';
14+
my $data = decode_json($res->content);
15+
is $data->{status}, 'healthy', 'has correct status';
16+
};
17+
18+
done_testing;

0 commit comments

Comments
 (0)