Skip to content

Commit

Permalink
Merge pull request #89 from abenerd/feature/simple-health-check
Browse files Browse the repository at this point in the history
Add Simple Health Check Endpoint
  • Loading branch information
freekmurze authored Apr 18, 2022
2 parents a0c9f0b + 6b3ae72 commit 680c572
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/basic-usage/endpoints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: Endpoints
weight: 5
---

The package offers a couple of controllers that can be used to check the health of your application.


## A beautiful status page

The `HealthCheckResultsController` will display a beautiful page with all health check results. You can find more detailed information on this page [here](/docs/viewing-results/on-a-webpage).

## Simple health check

The `SimpleHealthCheckController` will return either a status of `200` for a healthy application
or `503` for a unhealthy one without exposing any sensitive information about your application.

This is particularly helpful when you want to check the readiness of a container or a pod as they infer this by the responses status code.

```php
Route::get('health', \Spatie\Health\Http\Controllers\SimpleHealthCheckController::class);
```

## Detailed health check

Alternatively you can also register the `HealthCheckJsonResultsController`, this one will give you a detailed view of all
the checks that have been run with their status and meta data. This endpoint will also always return a status of `200` unless
something really goes wrong.

If you don't want to expose this info, you can add an `auth` middleware.

```php
Route::middleware('auth')->get('health', \Spatie\Health\Http\Controllers\HealthCheckJsonResultsController::class);
```
30 changes: 30 additions & 0 deletions src/Http/Controllers/SimpleHealthCheckController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Spatie\Health\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Artisan;
use Spatie\Health\Commands\RunHealthChecksCommand;
use Spatie\Health\ResultStores\ResultStore;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;

class SimpleHealthCheckController
{
public function __invoke(Request $request, ResultStore $resultStore): Response
{
if ($request->has('fresh') || config('health.oh_dear_endpoint.always_send_fresh_results')) {
Artisan::call(RunHealthChecksCommand::class);
}

if (! ($resultStore->latestResults()?->allChecksOk())) {
throw new ServiceUnavailableHttpException(message: 'Application not healthy');
}

return response([
'healthy' => true,
])
->header('Content-Type', 'application/json')
->header('Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
}
}
45 changes: 45 additions & 0 deletions tests/Http/Controllers/SimpleHealthCheckControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use function Pest\Laravel\artisan;
use function Pest\Laravel\getJson;
use Spatie\Health\Commands\RunHealthChecksCommand;
use Spatie\Health\Facades\Health;
use Spatie\Health\Http\Controllers\SimpleHealthCheckController;
use Spatie\Health\Tests\TestClasses\FakeRedisCheck;
use function Spatie\PestPluginTestTime\testTime;
use function Spatie\Snapshots\assertMatchesSnapshot;
use Symfony\Component\HttpFoundation\Response;

beforeEach(function () {
testTime()->freeze('2021-01-01 00:00:00');

Route::get('/', SimpleHealthCheckController::class);

$this->check = FakeRedisCheck::new()->replyWith(fn () => true);

Health::checks([
$this->check,
]);
});

it('will return a 200 status for a healthy check', function () {
artisan(RunHealthChecksCommand::class);

$json = getJson('/')
->assertOk()
->json();

assertMatchesSnapshot($json);
});

it('will return a 503 status for a unhealthy check', function () {
$this->check->replyWith(fn () => false);

artisan(RunHealthChecksCommand::class);

$json = getJson('/')
->assertStatus(Response::HTTP_SERVICE_UNAVAILABLE)
->json();

assertMatchesSnapshot($json);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
healthy: true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
message: 'Application not healthy'

0 comments on commit 680c572

Please sign in to comment.