feat: harden identity and recovery operations
This commit is contained in:
43
app/Http/Controllers/HealthController.php
Normal file
43
app/Http/Controllers/HealthController.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Throwable;
|
||||
|
||||
final class HealthController extends Controller
|
||||
{
|
||||
public function live(): JsonResponse
|
||||
{
|
||||
return response()
|
||||
->json(['status' => 'ok'])
|
||||
->header('Cache-Control', 'no-store');
|
||||
}
|
||||
|
||||
public function ready(): JsonResponse
|
||||
{
|
||||
try {
|
||||
DB::select('select 1');
|
||||
|
||||
$key = 'health:'.bin2hex(random_bytes(12));
|
||||
Cache::put($key, 'ok', 5);
|
||||
$cacheReady = Cache::pull($key) === 'ok';
|
||||
|
||||
if (! $cacheReady) {
|
||||
throw new \RuntimeException('Cache read/write check failed.');
|
||||
}
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
|
||||
return response()
|
||||
->json(['status' => 'not_ready'], 503)
|
||||
->header('Cache-Control', 'no-store');
|
||||
}
|
||||
|
||||
return response()
|
||||
->json(['status' => 'ready'])
|
||||
->header('Cache-Control', 'no-store');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user