Skip to content

Commit

Permalink
Added a maintenance mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtronics committed Jan 21, 2025
1 parent b49cb5e commit 36ce84b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
# Set to 1 on production to disable watermarks and hints on webpages
IS_PRODUCTIVE=0

# Set to 1 to enable maintenance mode, where no frontend is available (but backend login is still possible)
MAINTENANCE_MODE=0

SUPPORT_EMAIL=[email protected]
HHV_EMAIL=[email protected]
# The email where the forms get sent to, when two stura financers confirmed a payment order
Expand Down
52 changes: 52 additions & 0 deletions src/EventListener/MaintenanceModeListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);


namespace App\EventListener;

use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Twig\Environment;

#[AsEventListener]
final class MaintenanceModeListener
{

public const EXLUDED_PATHS = [
'/admin',
'/login',
'/logout',
'/2fa',
'/_wdt',
'/_profiler',
];

public function __construct(private Environment $twig,
#[Autowire(env: 'bool:MAINTENANCE_MODE')]
private readonly bool $enabled = false)
{

}

public function __invoke(RequestEvent $event): void
{
if (!$this->enabled) {
return;
}

//Exclude certain paths from maintenance mode
$path = $event->getRequest()->getPathInfo();
foreach (self::EXLUDED_PATHS as $excludedPath) {
if (str_starts_with($path, $excludedPath)) {
return;
}
}

$response = new Response($this->twig->render('maintenance.html.twig'), Response::HTTP_SERVICE_UNAVAILABLE);

$event->setResponse($response);
}
}
30 changes: 30 additions & 0 deletions templates/maintenance.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="{{ app.request.locale }}">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<style>
body { text-align: center; padding: 150px; }
h1 { font-size: 50px; }
body { font: 20px Helvetica, sans-serif; color: #333; }
article { display: block; text-align: left; width: 650px; margin: 0 auto; }
a { color: #dc8100; text-decoration: none; }
a:hover { color: #333; text-decoration: none; }
</style>

<title>Wartungsmodus</title>
</head>
<body>
<h1>Wartungsmodus</h1>
<div>
<p>Wir führen gerade Wartungsarbeiten durch. Versuche es bitte später erneut!</p>
<p>&mdash; Admin</p>
</div>

<div style="position: fixed; bottom: 5em;">
<hr>
<p><a href="{{ path("app_login") }}">Backend Login</a></p>
</div>
</body>

0 comments on commit 36ce84b

Please sign in to comment.