<?php
    header("Access-Control-Allow-Origin: http://localhost:8080");
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
    header("Access-Control-Allow-Headers: Content-Type, Authorization");

    $rootDirectoryPath = realpath(dirname(__FILE__));
    define ('__ROOT_DIR', $rootDirectoryPath );

    require_once(__ROOT_DIR . "/config/config.php");
    require_once(__ROOT_DIR . '/classes/AutoLoader.class.php');

    $request = Request::getCurrentRequest();
    Response::interceptEchos();

    try {
        $controller = Dispatcher::dispatch($request);
        $response = $controller->execute();
    } catch (Exception $e) {
        $log = Response::getEchos();
        $log .= " " . $e->getMessage();
        $response = Response::errorResponse($log);
    }

    $response->send();
?>