From 54dd4bb7a6ceff54486a39dee06159fdc22376b1 Mon Sep 17 00:00:00 2001 From: Hank Date: Fri, 29 May 2026 01:57:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20/health=20?= =?UTF-8?q?=E7=AB=AF=E9=BB=9E=E4=BE=9B=20UptimeRobot=20=E7=9B=A3=E6=8E=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 檢查 DB 連線、Redis ping、Cache 讀寫,任一失敗回傳 503。 回應包含 timestamp、environment、PHP/Laravel 版本、記憶體用量。 Co-Authored-By: Claude Sonnet 4.6 --- routes/web.php | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/routes/web.php b/routes/web.php index 86a06c5..d414f41 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,7 +1,40 @@ false, 'redis' => false, 'cache' => false]; + + try { + DB::connection()->getPdo(); + $checks['db'] = true; + } catch (\Throwable) {} + + try { + Redis::ping(); + $checks['redis'] = true; + } catch (\Throwable) {} + + try { + cache()->put('health_check', 'ok', 5); + $checks['cache'] = cache()->get('health_check') === 'ok'; + } catch (\Throwable) {} + + $healthy = $checks['db'] && $checks['redis'] && $checks['cache']; + + return response()->json([ + 'status' => $healthy ? 'ok' : 'degraded', + 'timestamp' => now()->toIso8601String(), + 'environment' => app()->environment(), + 'php_version' => PHP_VERSION, + 'laravel' => app()->version(), + 'memory_mb' => round(memory_get_usage(true) / 1024 / 1024, 2), + 'checks' => $checks, + ], $healthy ? 200 : 503); +}); + Route::get('/', function () { return view('welcome'); });