70 lines
2.6 KiB
PHP
70 lines
2.6 KiB
PHP
<?php
|
|
|
|
use App\Http\Middleware\CheckPermission;
|
|
use App\Http\Middleware\EnsureBawahanPresensi;
|
|
use App\Http\Middleware\SetPermissionsTeamId;
|
|
use App\Http\Middleware\UntanSSO;
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Foundation\Application;
|
|
use Illuminate\Foundation\Configuration\Exceptions;
|
|
use Illuminate\Foundation\Configuration\Middleware;
|
|
use Illuminate\Http\Request;
|
|
|
|
return Application::configure(basePath: dirname(__DIR__))
|
|
->withRouting(
|
|
web: __DIR__.'/../routes/web.php',
|
|
api: __DIR__.'/../routes/api.php',
|
|
commands: __DIR__.'/../routes/console.php',
|
|
health: '/up',
|
|
)
|
|
->withMiddleware(function (Middleware $middleware): void {
|
|
$middleware->encryptCookies(except: [
|
|
'untan_sso_token',
|
|
]);
|
|
|
|
$middleware->alias([
|
|
'permission' => CheckPermission::class,
|
|
'bawahan.presensi' => EnsureBawahanPresensi::class,
|
|
]);
|
|
|
|
// Trust proxies for HTTPS forwarding
|
|
$middleware->trustProxies(at: '*');
|
|
|
|
// Add SetPermissionsTeamId middleware to web group
|
|
$middleware->web(append: [
|
|
SetPermissionsTeamId::class,
|
|
UntanSSO::class,
|
|
]);
|
|
})
|
|
->withExceptions(function (Exceptions $exceptions): void {
|
|
// Route API selalu balas JSON (mis. error validasi 422), bukan redirect ke login.
|
|
$exceptions->shouldRenderJsonWhen(
|
|
fn ($request, $e) => $request->is('api/*') || $request->expectsJson()
|
|
);
|
|
|
|
// Koneksi ke database gagal (SQLSTATE 08006), biasanya karena diakses dari luar jaringan UNTAN.
|
|
$exceptions->render(function (QueryException $e, Request $request) {
|
|
if (! str_contains($e->getMessage(), 'SQLSTATE[08006]')) {
|
|
return null;
|
|
}
|
|
|
|
preg_match('/connection to server at "([^"]+)"/', $e->getMessage(), $cocok);
|
|
$host = $cocok[1] ?? config('database.connections.'.$e->getConnectionName().'.host', 'database');
|
|
$pesan = "Tidak dapat menghubungkan server {$host} dengan jaringan public, silahkan gunakan jaringan UNTAN";
|
|
|
|
if ($request->is('api/*') || $request->expectsJson()) {
|
|
return response()->json([
|
|
'status' => 'warning',
|
|
'message' => $pesan,
|
|
], 503);
|
|
}
|
|
|
|
// Hindari redirect berulang bila halaman sebelumnya juga membutuhkan database.
|
|
if (url()->previous() === $request->fullUrl()) {
|
|
return null;
|
|
}
|
|
|
|
return back()->with('warning', $pesan);
|
|
});
|
|
})->create();
|