bindAPI/public/index.php

50 lines
1.3 KiB
PHP

<?php declare(strict_types=1);
namespace App\Controller;
use Exception;
error_reporting(error_level: E_ALL);
require dirname(path: __DIR__) . '/vendor/autoload.php';
// read config
$configFile = dirname(path: __DIR__) . "/config.json";
$configJSON = file_get_contents(filename: $configFile);
$config = json_decode(json: $configJSON, associative: true);
// TODO only valid clients?
header(header: "Access-Control-Allow-Origin: *");
header(header: "Content-Type: application/json; charset=UTF-8");
header(header: "Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE");
header(header: "Access-Control-Max-Age: 3600");
header(header: "Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// TODO make a log class
$oFile = fopen(filename: 'log.txt', mode: 'a');
$uri = parse_url(url: $_SERVER['REQUEST_URI'], component: PHP_URL_PATH);
fputs(stream: $oFile, data: $uri . PHP_EOL);
fclose(stream: $oFile);
$uri = explode(separator: '/', string: $uri);
if ($uri[1] !== 'api') {
header(header: "HTTP/1.1 404 Not Found");
exit();
}
$requestMethod = $_SERVER["REQUEST_METHOD"];
try {
$controller = new RequestController(config: $config, requestMethod: $requestMethod, uri: $uri);
$controller->processRequest();
} catch (Exception $e) {
echo json_encode(value: [
'error' => $e->getMessage()
]);
}