diff --git a/app/Http/Controllers/HealthController.php b/app/Http/Controllers/HealthController.php index c75b903b0b..dac1f17bf4 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' => 'database connection failed', + ], 500); + + } + + + } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 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.php index 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 CheckForSetup return $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.php index 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']