Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Simple Health Check Endpoint #89

Merged
merged 6 commits into from
Apr 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config/health.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@
'url' => '/oh-dear-health-check-results',
],

'simple_health_check_endpoint' => [
'enabled' => false,

/*
* When this option is enabled, the checks will run before sending a response.
* Otherwise, we'll send the results from the last time the checks have run.
*/
'always_send_fresh_results' => true,

/*
* The URL that should be configured in the Application health settings at Oh Dear.
*/
'url' => '/simple-health-check',
],

/*
* You can set a theme for the local results page
*
Expand Down
17 changes: 17 additions & 0 deletions src/HealthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Spatie\Health\Components\Logo;
use Spatie\Health\Components\StatusIndicator;
use Spatie\Health\Http\Controllers\HealthCheckJsonResultsController;
use Spatie\Health\Http\Controllers\SimpleHealthCheckController;
use Spatie\Health\Http\Middleware\RequiresSecret;
use Spatie\Health\ResultStores\ResultStore;
use Spatie\Health\ResultStores\ResultStores;
Expand Down Expand Up @@ -47,6 +48,7 @@ public function packageBooted(): void
$this->app->make(Health::class)->inlineStylesheet(file_get_contents(__DIR__.'/../resources/dist/health.min.css'));

$this->registerOhDearEndpoint();
$this->registerSimpleHealthCheckEndpoint();
}

protected function registerOhDearEndpoint(): self
Expand All @@ -68,4 +70,19 @@ protected function registerOhDearEndpoint(): self

return $this;
}

protected function registerSimpleHealthCheckEndpoint(): self
{
if (! config('health.simple_health_check_endpoint.enabled')) {
return $this;
}

if (! config('health.oh_dear_endpoint.url')) {
return $this;
}

Route::get(config('health.simple_health_check_endpoint.url'), SimpleHealthCheckController::class);

return $this;
}
}
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'