bindAPI/public/index.php

43 lines
1.1 KiB
PHP

<?php
require '../vendor/autoload.php';
// read config
$configFile = dirname(__DIR__) . "/config.json";
$configJSON = file_get_contents($configFile);
$config = json_decode($configJSON, associative: true);
use App\Controller\DatabaseConnection;
use App\Controller\RequestController;
// TODO only valid clients?
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// TODO make a log class
$oFile = fopen ('log.txt', 'a');
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
fputs($oFile, $uri . PHP_EOL);
fclose($oFile);
$uri = explode( '/', $uri );
if ($uri[1] !== 'api') {
header("HTTP/1.1 404 Not Found");
exit();
}
$requestMethod = $_SERVER["REQUEST_METHOD"];
$dbConnection = (new DatabaseConnection(config: $config))->getConnection();
$controller = new RequestController($dbConnection, $requestMethod, $uri);
$controller->processRequest();