This commit is contained in:
2025-11-30 03:24:14 +01:00
parent 337f65205d
commit c7e355c1ea
5 changed files with 112 additions and 54 deletions

View File

@@ -3,7 +3,7 @@
// Optional: zentrale Config laden (wenn du magst) // Optional: zentrale Config laden (wenn du magst)
declare(strict_types=1); declare(strict_types=1);
require __DIR__ . '/../config/fileload.php'; require $_SERVER['DOCUMENT_ROOT']. '/../config/fileload.php';
// Basis-Header (CORS, JSON) // Basis-Header (CORS, JSON)
header('Content-Type: application/json; charset=utf-8'); header('Content-Type: application/json; charset=utf-8');
@@ -40,12 +40,12 @@ if ($path === '/') {
// Routing nach Bereich // Routing nach Bereich
if (str_starts_with($path, '/v1/')) { if (str_starts_with($path, '/v1/')) {
require __DIR__ . '/router.v1.php'; require __DIR__ . '/router/router.v1.php';
exit; exit;
} }
if (str_starts_with($path, '/internal/')) { if (str_starts_with($path, '/internal/')) {
require __DIR__ . '/router.internal.php'; require __DIR__ . '/router/router.internal.php';
exit; exit;
} }

View File

@@ -1,43 +0,0 @@
<?php
// /api/router.v1.php
declare(strict_types=1);
// Pfad erneut auslesen
$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
$path = rtrim($uri, '/');
// Namespace v1
switch ($path) {
case '/v1/quickcheck':
require __DIR__ . '/v1/target/quickcheck.php'; // dein bestehendes File
if (!function_exists('quickcheck_handle_request')) {
http_response_code(500);
echo json_encode(['ok' => false, 'error' => 'Handler quickcheck_handle_request not found']);
exit;
}
$result = quickcheck_handle_request();
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
break;
case '/v1/browser.quick.test':
require __DIR__ . '/v1/result/browser.quick.test.php';
if (!function_exists('browser_quick_test_handle_request')) {
http_response_code(500);
echo json_encode(['ok' => false, 'error' => 'Handler browser_quick_test_handle_request not found']);
exit;
}
browser_quick_test_handle_request();
break;
default:
http_response_code(404);
echo json_encode([
'ok' => false,
'error' => 'Unknown v1 endpoint',
'path' => $path,
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
break;
}

View File

@@ -27,9 +27,6 @@ if (
$uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH); $uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
$path = rtrim($uri, '/'); $path = rtrim($uri, '/');
// DB einbinden (für interne Tools brauchen wir oft DB)
require $_SERVER['DOCUMENT_ROOT'] . '/../config/db.php';
// interne Routen // interne Routen
switch ($path) { switch ($path) {
// Beispiel: Aggregierte Stats // Beispiel: Aggregierte Stats

109
api/router/router.v1.php Normal file
View File

@@ -0,0 +1,109 @@
<?php
// /api/router.v1.php
declare(strict_types=1);
/**
* Router für /v1/...
*
* Wird von /api/index.php aufgerufen:
* router_v1_dispatch($segments)
*
* $segments[0] ist dann z.B. "browser.quick.test" oder "quickcheck"
*/
function router_v1_dispatch(array $segments): void
{
if (empty($segments[0])) {
http_response_code(404);
echo json_encode([
'ok' => false,
'error' => 'No endpoint specified for v1',
], JSON_UNESCAPED_UNICODE);
return;
}
$endpoint = $segments[0]; // z.B. "browser.quick.test" oder "quickcheck"
switch ($endpoint) {
case 'quickcheck':
$file = require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/db.php'; // stellt $pdo (PDO) bereit
'/v1/target/quickcheck.php';
$handler = 'quickcheck_handle_request';
break;
case 'browser.quick.test':
$file = require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/db.php'; // stellt $pdo (PDO) bereit
'/v1/result/browser.quick.test.php';
$handler = 'browser_quick_test_handle_request';
break;
default:
http_response_code(404);
echo json_encode([
'ok' => false,
'error' => 'Unknown v1 endpoint',
'endpoint'=> $endpoint,
], JSON_UNESCAPED_UNICODE);
return;
}
if (!file_exists($file)) {
http_response_code(500);
echo json_encode([
'ok' => false,
'error' => 'Endpoint file not found',
'file' => basename($file),
], JSON_UNESCAPED_UNICODE);
return;
}
require_once $file;
if (!function_exists($handler)) {
http_response_code(500);
echo json_encode([
'ok' => false,
'error' => 'Handler not found',
'handler' => $handler,
], JSON_UNESCAPED_UNICODE);
return;
}
try {
$result = $handler();
// Falls der Handler mal kein Array zurückgibt
if (!is_array($result)) {
$result = [
'ok' => false,
'error' => 'Handler did not return array',
'raw' => $result,
'handler'=> $handler,
];
}
// HTTP-Status aus Ergebnis ableiten (optional)
if (isset($result['ok']) && $result['ok'] === false) {
// Bei Fehler eher 400 als 200, außer du willst es anders
if (!http_response_code()) {
http_response_code(400);
}
} else {
if (!http_response_code()) {
http_response_code(200);
}
}
echo json_encode($result, JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
http_response_code(500);
error_log('[usbcheck] router_v1_dispatch error: ' . $e->getMessage());
echo json_encode([
'ok' => false,
'error' => 'Unhandled exception in endpoint',
'debug' => $e->getMessage(), // später ggf. entfernen
], JSON_UNESCAPED_UNICODE);
}
}

View File

@@ -20,11 +20,6 @@ function browser_quick_test_handle_request(): array
session_start(); session_start();
} }
// DB einbinden Pfad abhängig vom VHost:
// api.{staging.}usbcheck.it → DocumentRoot = .../api
// config liegt eine Ebene darüber: ../config/db.php
require_once $_SERVER['DOCUMENT_ROOT'] . '/../config/db.php'; // stellt $pdo (PDO) bereit
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// 1. JSON einlesen // 1. JSON einlesen
// --------------------------------------------------------------------- // ---------------------------------------------------------------------