custom-prometheus-exporter/index.php
Jeroen De Meerleer 8af61f97b0
Added /config endpoint and link in 404 page
The update includes the addition of a new '/config' endpoint in index.php, which returns the application's configuration as a JSON response. A corresponding link to this new endpoint has also been added to the 404 error page.
2024-06-13 17:43:32 +02:00

29 lines
1.1 KiB
PHP

<?php
if($_SERVER["REQUEST_URI"] == '/metrics') {
header('Content-Type: text/plain; version=0.0.4');
$config = getenv('CONFIG_FILE');
$phpbin = getenv('PHP_BINARY');
$output = shell_exec('CONFIG_FILE="' . $config . '" ' . $phpbin . ' ' . $_SERVER['DOCUMENT_ROOT'] . 'script.php');
echo $output;
} elseif ($_SERVER["REQUEST_URI"] == '/config') {
header('Content-Type: application/json');
require_once getenv('CONFIG_FILE');
echo json_encode($config);
} elseif ($_SERVER["REQUEST_URI"] == '/version') {
header('Content-Type: application/json');
$git = [
'lastCommit' => trim(shell_exec('git rev-parse HEAD')),
'lastCommitTimestamp' => trim(shell_exec('git log -1 --format=%cd')),
'branch' => trim(shell_exec('git rev-parse --abbrev-ref HEAD')),
];
echo json_encode($git);
} else {
require_once('vendor/autoload.php');
http_response_code(404);
$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
'cache' => 'twig_cache',
]);
echo $twig->render('404.twig', ['path' => $_SERVER["REQUEST_URI"]]);
}