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,9 +1,9 @@
<?php <?php
// /api/index.php // /api/index.php
// Optional: zentrale Config laden (wenn du magst)
declare(strict_types=1); declare(strict_types=1);
$apibasedir = $_SERVER['DOCUMENT_ROOT'];
$apibasedir = $_SERVER['DOCUMENT_ROOT']; // bei dir: /.../projects/usbcheck/staging/api
require $apibasedir . '/../config/fileload.php'; require $apibasedir . '/../config/fileload.php';
// Basis-Header (CORS, JSON) // Basis-Header (CORS, JSON)
@@ -41,7 +41,13 @@ if ($path === '/') {
// Routing nach Bereich // Routing nach Bereich
if (str_starts_with($path, '/v1/')) { if (str_starts_with($path, '/v1/')) {
// 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'; require_once $apibasedir . '/router/router.v1.php';
router_v1_dispatch($segments);
exit; exit;
} }

View File

@@ -6,16 +6,19 @@ declare(strict_types=1);
/** /**
* Router für /v1/... * Router für /v1/...
* *
* Wird von /api/index.php aufgerufen: * Wird von /api/index.php so aufgerufen:
* router_v1_dispatch($segments, $apibasedir) * router_v1_dispatch($segments)
* *
* $segments[0] ist dann z.B. "browser.quick.test" oder "quickcheck" * $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])) { if (empty($segments[0])) {
http_response_code(404); http_response_code(404);
echo json_encode([ echo json_encode([
@@ -73,7 +76,6 @@ function router_v1_dispatch(array $segments, string $apibasedir): void
try { try {
$result = $handler(); $result = $handler();
// Falls der Handler mal kein Array zurückgibt
if (!is_array($result)) { if (!is_array($result)) {
$result = [ $result = [
'ok' => false, '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 (isset($result['ok']) && $result['ok'] === false) {
if (!http_response_code()) { if (!http_response_code()) {
http_response_code(400); http_response_code(400);