A free open source IT asset/license management system
Fixed #15439 - check database on healthcheck
snipe4d9e85026aa8
diff --git a/app/Http/Controllers/HealthController.php b/app/Http/Controllers/HealthController.phpindex c75b903b0b..2a2904e0fb 100644--- a/app/Http/Controllers/HealthController.php+++ b/app/Http/Controllers/HealthController.php@@ -3,6 +3,7 @@namespace App\Http\Controllers;use Illuminate\Routing\Controller as BaseController;+use Illuminate\Support\Facades\DB;/*** This controller provide the health route for@@ -15,13 +16,35 @@ use Illuminate\Routing\Controller as BaseController;*/class HealthController extends BaseController{++ public function __construct()+ {+ $this->middleware('health');+ }++/*** Returns a fixed JSON content ({ "status": "ok"}) which indicate the app is up and running*/public function get(){- return response()->json([- 'status' => 'ok',- ]);+ try {++ if (DB::select('select 2 + 2')) {+ return response()->json([+ 'status' => 'ok',+ ]);+ }++ } catch (\Exception $e) {+ \Log::error('Could not connect to database');+ return response()->json([+ 'status' => 'Could not connect to database',+ ], 500);++ }++ die();+}}diff --git a/app/Http/Kernel.php b/app/Http/Kernel.phpindex 8c9289a799..b69e22e4f9 100644--- a/app/Http/Kernel.php+++ b/app/Http/Kernel.php@@ -53,6 +53,10 @@ class Kernel extends HttpKernel\App\Http\Middleware\CheckLocale::class,\Illuminate\Routing\Middleware\SubstituteBindings::class,],++ 'health' => [++ ],];/**@@ -69,5 +73,6 @@ class Kernel extends HttpKernel'can' => \Illuminate\Auth\Middleware\Authorize::class,'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,+ 'health' => null,];}diff --git a/app/Http/Middleware/CheckForSetup.php b/app/Http/Middleware/CheckForSetup.phpindex 4e399ffcff..6cb593b5ff 100644--- a/app/Http/Middleware/CheckForSetup.php+++ b/app/Http/Middleware/CheckForSetup.php@@ -7,14 +7,19 @@ use Closure;class CheckForSetup{++ protected $except = [+ '_debugbar*',+ 'health'+ ];+public function handle($request, Closure $next, $guard = null){/**- * This is dumb- * @todo Check on removing this, not sure if it's still needed+ * Skip this middleware for the debugbar and health check*/- if ($request->is('_debugbar*')) {+ if ($request->is($this->except)) {return $next($request);}@@ -25,7 +30,7 @@ class CheckForSetupreturn $next($request);}} else {- if (! ($request->is('setup*')) && ! ($request->is('.env')) && ! ($request->is('health'))) {+ if (! ($request->is('setup*')) && ! ($request->is('.env'))) {return redirect(config('app.url').'/setup');}diff --git a/routes/web.php b/routes/web.phpindex f3ba8350f2..60b74476c7 100644--- a/routes/web.php+++ b/routes/web.php@@ -536,13 +536,16 @@ Route::group(['middleware' => 'web'], function () {)->name('logout.post');});-//Auth::routes();-Route::get(- '/health',+/**+ * Health check route - skip middleware+ */+Route::withoutMiddleware(['web'])->get(+ '/health',[HealthController::class, 'get'])->name('health');+Route::middleware(['auth'])->get('/',[DashboardController::class, 'index']