Skip to content

Commit

Permalink
Added healthcheck endpoints; Fixed filament check
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Apr 12, 2024
1 parent 67aa7e8 commit 27bffaa
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
60 changes: 60 additions & 0 deletions app/Http/Controllers/Web/HealthCheckController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class HealthCheckController extends Controller
{
/**
* Check if the application is up and running
* This check does not check the database or cache connectivity
*/
public function up(): JsonResponse
{
return response()->json([
'success' => true,
]);
}

/**
* Debug information for the application
* This check checks the database and cache connectivity
*/
public function debug(Request $request): JsonResponse
{
// Check database connectivity
User::query()->count();

// Check cache connectivity
Cache::put('health-check', Carbon::now()->timestamp);

// Check ip address correct behind load balancer
$ipAddress = $request->ip();
$hostname = $request->getHost();
$secure = $request->secure();
$isTrustedProxy = $request->isFromTrustedProxy();

$dbTimezone = DB::select('show timezone;');

return response()
->json([
'ip_address' => $ipAddress,
'hostname' => $hostname,
'timestamp' => Carbon::now()->timestamp,
'date_time_utc' => Carbon::now('UTC')->toDateTimeString(),
'date_time_app' => Carbon::now()->toDateTimeString(),
'timezone' => $dbTimezone[0]->TimeZone,
'secure' => $secure,
'is_trusted_proxy' => $isTrustedProxy,
]);
}
}
5 changes: 3 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use App\Enums\Weekday;
use Database\Factories\UserFactory;
use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
Expand Down Expand Up @@ -37,7 +38,7 @@
* @method static Builder<User> query()
* @method Builder<User> belongsToOrganization(Organization $organization)
*/
class User extends Authenticatable
class User extends Authenticatable implements FilamentUser
{
use HasApiTokens;
use HasFactory;
Expand Down Expand Up @@ -104,7 +105,7 @@ class User extends Authenticatable

public function canAccessPanel(Panel $panel): bool
{
return in_array($this->email, config('auth.super_admins', []), true);
return in_array($this->email, config('auth.super_admins', []), true) && $this->hasVerifiedEmail();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use App\Http\Controllers\Web\DashboardController;
use App\Http\Controllers\Web\HealthCheckController;
use App\Http\Controllers\Web\HomeController;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
Expand Down Expand Up @@ -59,3 +60,6 @@
})->name('tags');

});

Route::get('health-check/up', [HealthCheckController::class, 'up']);
Route::get('health-check/debug', [HealthCheckController::class, 'debug']);

0 comments on commit 27bffaa

Please sign in to comment.