esv-bludenz-php/router.php

46 lines
1.3 KiB
PHP

<?php
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$root = __DIR__;
$public = $root . '/public';
$build = $root . '/build';
$staticBase = $build;
if ($path !== '/' && file_exists($staticBase . $path) && !is_dir($staticBase . $path)) {
$file = $staticBase . $path;
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$types = [
'js' => 'application/javascript',
'css' => 'text/css',
'map' => 'application/json',
'json' => 'application/json',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'svg' => 'image/svg+xml',
'ico' => 'image/x-icon',
'txt' => 'text/plain',
'webmanifest' => 'application/manifest+json',
];
if (isset($types[$ext])) {
header('Content-Type: ' . $types[$ext]);
}
readfile($file);
return true;
}
if ($path !== '/' && file_exists($public . $path) && !is_dir($public . $path)) {
return false;
}
if (str_starts_with($path, '/api/')) {
$apiFile = $root . $path;
if (file_exists($apiFile) && !is_dir($apiFile)) {
require $apiFile;
return true;
}
http_response_code(404);
echo 'API route not found';
return true;
}
require $build . '/index.html';