This commit is contained in:
2025-11-30 21:57:51 +01:00
parent 14ddfba2a4
commit b308bc106c
2 changed files with 19 additions and 12 deletions

View File

@@ -1,10 +1,10 @@
<?php
// /api/index.php
// Optional: zentrale Config laden (wenn du magst)
declare(strict_types=1);
$apibasedir = $_SERVER['DOCUMENT_ROOT'];
require $apibasedir. '/../config/fileload.php';
$apibasedir = $_SERVER['DOCUMENT_ROOT']; // bei dir: /.../projects/usbcheck/staging/api
require $apibasedir . '/../config/fileload.php';
// Basis-Header (CORS, JSON)
header('Content-Type: application/json; charset=utf-8');
@@ -41,12 +41,18 @@ if ($path === '/') {
// Routing nach Bereich
if (str_starts_with($path, '/v1/')) {
require_once $apibasedir.'/router/router.v1.php';
// alles hinter /v1/ in Segmente zerlegen
$rel = substr($path, strlen('/v1/')); // z.B. "browser.quick.test" oder "quickcheck" oder "foo/bar"
$rel = ltrim($rel, '/');
$segments = $rel === '' ? [] : explode('/', $rel);
require_once $apibasedir . '/router/router.v1.php';
router_v1_dispatch($segments);
exit;
}
if (str_starts_with($path, '/internal/')) {
require_once $apibasedir. '/router/router.internal.php';
require_once $apibasedir . '/router/router.internal.php';
exit;
}

View File

@@ -6,16 +6,19 @@ declare(strict_types=1);
/**
* Router für /v1/...
*
* Wird von /api/index.php aufgerufen:
* router_v1_dispatch($segments, $apibasedir)
* Wird von /api/index.php so aufgerufen:
* router_v1_dispatch($segments)
*
* $segments[0] ist dann z.B. "browser.quick.test" oder "quickcheck"
*
* $apibasedir ist der Pfad auf /api (also das Root des API-Vhosts).
*/
function router_v1_dispatch(array $segments, string $apibasedir): void
// Basisverzeichnis der API ermitteln (…/api)
$apibasedir = dirname(__DIR__);
function router_v1_dispatch(array $segments): void
{
global $apibasedir;
if (empty($segments[0])) {
http_response_code(404);
echo json_encode([
@@ -73,7 +76,6 @@ function router_v1_dispatch(array $segments, string $apibasedir): void
try {
$result = $handler();
// Falls der Handler mal kein Array zurückgibt
if (!is_array($result)) {
$result = [
'ok' => false,
@@ -83,7 +85,6 @@ function router_v1_dispatch(array $segments, string $apibasedir): void
];
}
// HTTP-Status aus Ergebnis ableiten (optional)
if (isset($result['ok']) && $result['ok'] === false) {
if (!http_response_code()) {
http_response_code(400);