Skip to content

Commit

Permalink
Reduce overhead of health check endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Sep 12, 2024
1 parent d3545b3 commit cc10af0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
3 changes: 3 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class Kernel extends HttpKernel
\Illuminate\Routing\Middleware\SubstituteBindings::class,
ForceJsonResponse::class,
],

'health-check' => [
],
];

/**
Expand Down
7 changes: 7 additions & 0 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Providers;

use App\Http\Controllers\Web\HealthCheckController;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -37,6 +38,12 @@ public function boot(): void
});

$this->routes(function () {
Route::middleware('health-check')
->group(function () {
Route::get('health-check/up', [HealthCheckController::class, 'up']);
Route::get('health-check/debug', [HealthCheckController::class, 'debug']);
});

Route::middleware('api')
->prefix('api')
->name('api.')
Expand Down
4 changes: 0 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use App\Http\Controllers\Web\DashboardController;
use App\Http\Controllers\Web\HealthCheckController;
use App\Http\Controllers\Web\HomeController;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
Expand Down Expand Up @@ -64,6 +63,3 @@
})->name('import');

});

Route::get('health-check/up', [HealthCheckController::class, 'up']);
Route::get('health-check/debug', [HealthCheckController::class, 'debug']);
7 changes: 7 additions & 0 deletions tests/Unit/Endpoint/Web/HealthCheckEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Unit\Endpoint\Web;

use App\Http\Controllers\Web\HealthCheckController;
use Illuminate\Support\Facades\DB;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;

Expand All @@ -14,10 +15,16 @@ class HealthCheckEndpointTest extends EndpointTestAbstract
{
public function test_up_endpoint_returns_ok(): void
{
// Arrange
DB::enableQueryLog();
DB::flushQueryLog();

// Act
$response = $this->get('health-check/up');
$queryLog = DB::getQueryLog();

// Assert
$this->assertCount(0, $queryLog);
$response->assertSuccessful();
$response->assertExactJson([
'success' => true,
Expand Down

0 comments on commit cc10af0

Please sign in to comment.