added support for config file

This commit is contained in:
tracer 2022-01-22 17:31:29 +01:00
parent 108bafe299
commit b3e8ff6ffb
1 changed files with 12 additions and 9 deletions

View File

@ -2,9 +2,17 @@
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");
@ -12,28 +20,23 @@ header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
$dbConnection = (new DatabaseConnection())->getConnection();
// TODO make a log class
$oFile = fopen ('log.txt', 'a');
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
fputs($oFile, $uri);
$uri = explode( '/', $uri );
fputs($oFile, $uri . PHP_EOL);
fclose($oFile);
$uri = explode( '/', $uri );
if ($uri[1] !== 'api') {
header("HTTP/1.1 404 Not Found");
exit();
}
// TODO check for valid API key
$requestMethod = $_SERVER["REQUEST_METHOD"];
$dbConnection = (new DatabaseConnection(config: $config))->getConnection();
// pass the request method and user ID to the PersonController and process the HTTP request:
$controller = new RequestController($dbConnection, $requestMethod, $uri);
$controller->processRequest();
fclose($oFile);