Skip to content

Commit

Permalink
Fixed #15439 - check database on healthcheck
Browse files Browse the repository at this point in the history
Signed-off-by: snipe <[email protected]>
  • Loading branch information
snipe committed Oct 2, 2024
1 parent 54fbd05 commit 4d9e850
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
29 changes: 26 additions & 3 deletions app/Http/Controllers/HealthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,13 +16,35 @@
*/
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();

}
}
5 changes: 5 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class Kernel extends HttpKernel
\App\Http\Middleware\CheckLocale::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

'health' => [

],
];

/**
Expand All @@ -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,
];
}
13 changes: 9 additions & 4 deletions app/Http/Middleware/CheckForSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@

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);
}

Expand All @@ -25,7 +30,7 @@ public function handle($request, Closure $next, $guard = null)
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');
}

Expand Down
9 changes: 6 additions & 3 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,16 @@
)->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']
Expand Down

0 comments on commit 4d9e850

Please sign in to comment.