Fixed #15439 - check database on healthcheck

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-10-02 15:36:01 +01:00
parent 54fbd0540d
commit 4d9e85026a
4 changed files with 46 additions and 10 deletions

View file

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController; use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
/** /**
* This controller provide the health route for * This controller provide the health route for
@ -15,13 +16,35 @@ use Illuminate\Routing\Controller as BaseController;
*/ */
class HealthController extends 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 * Returns a fixed JSON content ({ "status": "ok"}) which indicate the app is up and running
*/ */
public function get() public function get()
{ {
return response()->json([ try {
'status' => 'ok',
]); 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();
} }
} }

View file

@ -53,6 +53,10 @@ class Kernel extends HttpKernel
\App\Http\Middleware\CheckLocale::class, \App\Http\Middleware\CheckLocale::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class, \Illuminate\Routing\Middleware\SubstituteBindings::class,
], ],
'health' => [
],
]; ];
/** /**
@ -69,5 +73,6 @@ class Kernel extends HttpKernel
'can' => \Illuminate\Auth\Middleware\Authorize::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'health' => null,
]; ];
} }

View file

@ -7,14 +7,19 @@ use Closure;
class CheckForSetup class CheckForSetup
{ {
protected $except = [
'_debugbar*',
'health'
];
public function handle($request, Closure $next, $guard = null) public function handle($request, Closure $next, $guard = null)
{ {
/** /**
* This is dumb * Skip this middleware for the debugbar and health check
* @todo Check on removing this, not sure if it's still needed
*/ */
if ($request->is('_debugbar*')) { if ($request->is($this->except)) {
return $next($request); return $next($request);
} }
@ -25,7 +30,7 @@ class CheckForSetup
return $next($request); return $next($request);
} }
} else { } else {
if (! ($request->is('setup*')) && ! ($request->is('.env')) && ! ($request->is('health'))) { if (! ($request->is('setup*')) && ! ($request->is('.env'))) {
return redirect(config('app.url').'/setup'); return redirect(config('app.url').'/setup');
} }

View file

@ -536,13 +536,16 @@ Route::group(['middleware' => 'web'], function () {
)->name('logout.post'); )->name('logout.post');
}); });
//Auth::routes();
Route::get( /**
'/health', * Health check route - skip middleware
*/
Route::withoutMiddleware(['web'])->get(
'/health',
[HealthController::class, 'get'] [HealthController::class, 'get']
)->name('health'); )->name('health');
Route::middleware(['auth'])->get( Route::middleware(['auth'])->get(
'/', '/',
[DashboardController::class, 'index'] [DashboardController::class, 'index']