Update known paths in 404 error template based on available routes

- Adjusted handling of known paths to dynamically include additional routes if a specific directory exists, enhancing user experience.
This commit is contained in:
Jeroen De Meerleer 2024-06-14 20:49:33 +02:00
parent f3513ea80e
commit 265128d725
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
2 changed files with 10 additions and 5 deletions

View File

@ -10,7 +10,7 @@ if($_SERVER["REQUEST_URI"] == '/metrics') {
header('Content-Type: application/json'); header('Content-Type: application/json');
require_once getenv('CONFIG_FILE'); require_once getenv('CONFIG_FILE');
echo json_encode($config); echo json_encode($config);
} elseif ($_SERVER["REQUEST_URI"] == '/version') { } elseif ($_SERVER["REQUEST_URI"] == '/version' && is_dir($_SERVER['DOCUMENT_ROOT'] . '/.git')) {
header('Content-Type: application/json'); header('Content-Type: application/json');
$git = [ $git = [
'lastCommit' => trim(shell_exec('git rev-parse HEAD')), 'lastCommit' => trim(shell_exec('git rev-parse HEAD')),
@ -20,10 +20,15 @@ if($_SERVER["REQUEST_URI"] == '/metrics') {
echo json_encode($git); echo json_encode($git);
} else { } else {
require_once('vendor/autoload.php'); require_once('vendor/autoload.php');
$knownpaths = [
'/metrics',
'/config'
];
if (is_dir($_SERVER['DOCUMENT_ROOT'] . '/.git')) $knownpaths[] = '/version';
http_response_code(404); http_response_code(404);
$loader = new \Twig\Loader\FilesystemLoader('templates'); $loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [ $twig = new \Twig\Environment($loader, [
'cache' => 'twig_cache', 'cache' => 'twig_cache',
]); ]);
echo $twig->render('404.twig', ['path' => $_SERVER["REQUEST_URI"]]); echo $twig->render('404.twig', ['path' => $_SERVER["REQUEST_URI"], 'knownpaths' => $knownpaths]);
} }

View File

@ -7,8 +7,8 @@
</style> </style>
<body> <body>
<h1>404 Uri {{ path }} not found</h1> <h1>404 Uri {{ path }} not found</h1>
<p><a href="/metrics">/metrics</a></p> {% for knownpath in knownpaths %}
<p><a href="/config">/config</a></p> <p><a href="{{ knownpath }}">{{ knownpath }}</a></p>
<p><a href="/version">/version</a></p> {% endfor %}
</body> </body>
</html> </html>