bindAPI/public/index.php

43 lines
1.1 KiB
PHP
Raw Normal View History

2022-01-18 19:14:24 +01:00
<?php
require '../vendor/autoload.php';
2022-01-22 17:31:29 +01:00
// read config
$configFile = dirname(__DIR__) . "/config.json";
$configJSON = file_get_contents($configFile);
$config = json_decode($configJSON, associative: true);
2022-01-18 19:14:24 +01:00
use App\Controller\DatabaseConnection;
use App\Controller\RequestController;
2022-01-22 17:31:29 +01:00
// TODO only valid clients?
2022-01-18 19:14:24 +01:00
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);
2022-01-22 17:31:29 +01:00
fputs($oFile, $uri . PHP_EOL);
fclose($oFile);
2022-01-18 19:14:24 +01:00
2022-01-22 17:31:29 +01:00
$uri = explode( '/', $uri );
2022-01-18 19:14:24 +01:00
if ($uri[1] !== 'api') {
header("HTTP/1.1 404 Not Found");
exit();
}
$requestMethod = $_SERVER["REQUEST_METHOD"];
2022-01-22 17:31:29 +01:00
$dbConnection = (new DatabaseConnection(config: $config))->getConnection();
2022-01-18 19:14:24 +01:00
$controller = new RequestController($dbConnection, $requestMethod, $uri);
$controller->processRequest();