Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
98 changes: 98 additions & 0 deletions app/Http/Controllers/HealthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class HealthController extends Controller
{
public function ping(): Response
{
return response('pong!', 200)
->header('Content-Type', 'text/plain');
}

public function health(): JsonResponse
{
$checks = [
'database' => $this->checkDatabase(),
'redis' => $this->checkRedis(),
'disk' => $this->checkDisk(),
];

$healthy = collect($checks)->every(fn ($check) => $check['status'] === 'ok');

return response()->json([
'status' => $healthy ? 'healthy' : 'unhealthy',
'checks' => $checks,
'timestamp' => now()->toIso8601String(),
], $healthy ? 200 : 503);
}

private function checkDatabase(): array
{
try {
DB::connection()->getPdo();
DB::select('SELECT 1');

return ['status' => 'ok'];
} catch (\Exception $e) {
return [
'status' => 'error',
'message' => $e->getMessage(),
];
}
}

private function checkRedis(): array
{
try {
$response = Redis::ping();
if ($response === true || $response == 'PONG' || (is_object($response) && method_exists($response, 'getPayload') && $response->getPayload() === 'PONG')) {
return ['status' => 'ok'];
}

return [
'status' => 'error',
'message' => 'Unexpected response from Redis',
];
} catch (\Exception $e) {
return [
'status' => 'error',
'message' => $e->getMessage(),
];
}
}

private function checkDisk(): array
{
try {
$path = storage_path();
$freeBytes = disk_free_space($path);
$totalBytes = disk_total_space($path);

if ($freeBytes === false || $totalBytes === false) {
return [
'status' => 'error',
'message' => 'Unable to read disk space',
];
}

$usedPercent = round((($totalBytes - $freeBytes) / $totalBytes) * 100, 1);
$status = $usedPercent > 95 ? 'error' : ($usedPercent > 85 ? 'warning' : 'ok');

return [
'status' => $status,
'used_percent' => $usedPercent,
];
} catch (\Exception $e) {
return [
'status' => 'error',
'message' => $e->getMessage(),
];
}
}
}
5 changes: 5 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use App\Http\Controllers\AuthController;
use App\Http\Controllers\EmailChangeController;
use App\Http\Controllers\EmailVerificationController;
use App\Http\Controllers\HealthController;
use App\Http\Controllers\InboxController;
use App\Http\Controllers\InstanceActorController;
use App\Http\Controllers\NodeInfoController;
Expand All @@ -37,6 +38,10 @@
use App\Http\Middleware\FederationEnabled;
use Illuminate\Support\Facades\Route;

// Health check endpoints
Route::get('/ping', [HealthController::class, 'ping'])->name('health.ping');
Route::get('/health', [HealthController::class, 'health'])->name('health.check');

// NodeInfo endpoints
Route::group(['prefix' => 'nodeinfo'], function () {
Route::get('2.0', [NodeInfoController::class, 'nodeInfo20'])
Expand Down