Skip to content

Commit

Permalink
feat: add health endpoint (resolves #1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
greatislander committed Dec 1, 2024
1 parent d1adfcd commit 4f2b96a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/Http/Controllers/HealthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Events\DiagnosingHealth;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;

class HealthController extends Controller
{
public function show(Request $request): JsonResponse
{
$status = [];

Event::dispatch(new DiagnosingHealth);

if (defined('LARAVEL_START')) {
$status['duration'] = round((microtime(true) - LARAVEL_START) * 1000).'ms';
}

return response()->json($status);
}
}
3 changes: 3 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use App\Http\Controllers\HealthController;
use App\Http\Controllers\PageController;
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
Expand Down Expand Up @@ -160,6 +161,8 @@
->middleware(['auth'])
->name('users.destroy');

Route::get('/health', [HealthController::class, 'show'])->name('health');

require __DIR__.'/admin.php';
require __DIR__.'/block-list.php';
require __DIR__.'/individuals.php';
Expand Down
21 changes: 21 additions & 0 deletions tests/Feature/HealthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Testing\Fluent\AssertableJson;

use function Pest\Laravel\get;

test('health page loads without response', function () {
get(route('health'))
->assertOk()
->assertJson(fn (AssertableJson $json) => $json->missing('duration')
);
});

test('health page loads with response', function () {
define('LARAVEL_START', microtime(true));

get(route('health'))
->assertOk()
->assertJson(fn (AssertableJson $json) => $json->has('duration')
);
});

0 comments on commit 4f2b96a

Please sign in to comment.