web2print-service/public/index.php

83 lines
2.5 KiB
PHP
Raw Normal View History

2025-11-28 15:38:02 +01:00
<?php
// Autoloader simple
spl_autoload_register(function ($class) {
$prefix = 'Web2Print\\';
$baseDir = __DIR__ . '/../src/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relativeClass = substr($class, $len);
$file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
if (file_exists($file)) {
require $file;
}
});
// Charger la configuration
$config = require __DIR__ . '/../config/config.php';
// Router simple
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// Afficher la documentation sur la racine
if ($uri === '/' && $_SERVER['REQUEST_METHOD'] === 'GET') {
readfile(__DIR__ . '/docs.html');
exit;
}
// Définir le temps d'exécution max
set_time_limit($config['max_execution_time']);
// Headers CORS (si nécessaire)
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, X-API-Key');
// Gérer preflight OPTIONS
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit;
}
// Authentification
$auth = new \Web2Print\Middleware\AuthMiddleware($config);
if (!$auth->authenticate()) {
exit;
}
// Endpoints
2025-11-28 15:38:02 +01:00
if ($uri === '/generate') {
// Endpoint synchrone existant (rétro-compat)
2025-11-28 15:38:02 +01:00
$generator = new \Web2Print\Services\PdfGenerator($config);
$controller = new \Web2Print\Controllers\GenerateController($generator, $config);
$controller->handle();
} elseif ($uri === '/jobs') {
// POST /jobs : créer un job async
$jobs = new \Web2Print\Services\JobManager($config);
$controller = new \Web2Print\Controllers\JobsController($jobs, $config);
$controller->create();
} elseif (preg_match('#^/jobs/([a-f0-9]+)/result$#', $uri, $m)) {
// GET /jobs/{id}/result : récupérer le PDF
$jobs = new \Web2Print\Services\JobManager($config);
$controller = new \Web2Print\Controllers\JobsController($jobs, $config);
$controller->result($m[1]);
} elseif (preg_match('#^/jobs/([a-f0-9]+)$#', $uri, $m)) {
// GET /jobs/{id} : status — DELETE /jobs/{id} : cleanup
$jobs = new \Web2Print\Services\JobManager($config);
$controller = new \Web2Print\Controllers\JobsController($jobs, $config);
if ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
$controller->delete($m[1]);
} else {
$controller->status($m[1]);
}
2025-11-28 15:38:02 +01:00
} else {
http_response_code(404);
header('Content-Type: application/json');
echo json_encode(['error' => 'Not found']);
}