generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from abenerd/feature/simple-health-check
Add Simple Health Check Endpoint
- Loading branch information
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
tests/Http/Controllers/SimpleHealthCheckControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
1 change: 1 addition & 0 deletions
1
..._/SimpleHealthCheckControllerTest__it_will_return_a_200_status_for_a_healthy_check__1.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
healthy: true |
1 change: 1 addition & 0 deletions
1
...SimpleHealthCheckControllerTest__it_will_return_a_503_status_for_a_unhealthy_check__1.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
message: 'Application not healthy' |