web2print-service/public/index.php

63 lines
1.6 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;
}
// Endpoint de génération PDF
if ($uri === '/generate') {
$generator = new \Web2Print\Services\PdfGenerator($config);
$controller = new \Web2Print\Controllers\GenerateController($generator, $config);
$controller->handle();
} else {
http_response_code(404);
header('Content-Type: application/json');
echo json_encode(['error' => 'Not found']);
}