From bda4542cde8559a3b3574d19011b1004abdd116b Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Thu, 10 Oct 2024 20:32:37 +0200 Subject: [PATCH] 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. --- lib/MetaCPAN/Web/Controller/Root.pm | 10 ++++++++++ root/healthcheck.tx | 1 + t/controller/healthcheck.t | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 root/healthcheck.tx create mode 100644 t/controller/healthcheck.t diff --git a/lib/MetaCPAN/Web/Controller/Root.pm b/lib/MetaCPAN/Web/Controller/Root.pm index 3b281a959a..401857ba76 100644 --- a/lib/MetaCPAN/Web/Controller/Root.pm +++ b/lib/MetaCPAN/Web/Controller/Root.pm @@ -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. diff --git a/root/healthcheck.tx b/root/healthcheck.tx new file mode 100644 index 0000000000..1e3babd029 --- /dev/null +++ b/root/healthcheck.tx @@ -0,0 +1 @@ +[% { status => $status }.json() | raw %] diff --git a/t/controller/healthcheck.t b/t/controller/healthcheck.t new file mode 100644 index 0000000000..a916c9b49f --- /dev/null +++ b/t/controller/healthcheck.t @@ -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;