Gatekeeper/index.php

54 lines
1.4 KiB
PHP

<?php
// Autoload files using the Composer autoloader.
require_once __DIR__ . '/vendor/autoload.php';
// First Party
use Tor\Tor;
use Tor\Api;
use SeriousJSON\JsonDatabase;
// Third Party Routing Library
use Gac\Routing\Exceptions\CallbackNotFound;
use Gac\Routing\Exceptions\RouteNotFoundException;
use Gac\Routing\Request;
use Gac\Routing\Routes;
// Initialize an instance of the routing library
$routes = new Routes();
// Initialize Twig
// TODO: Only do this if needed
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . DIRECTORY_SEPARATOR . 'templates');
$twig = new \Twig\Environment($loader, [
'cache' => __DIR__ . DIRECTORY_SEPARATOR . 'compilation_cache',
]);
$data = new JsonDatabase(__DIR__ . DIRECTORY_SEPARATOR . 'data');
$tor = new Tor();
$api = new Api();
try {
// Initialize Database
$data->init();
// Initialize Tor in order to register routes
$tor->init();
// Initialize Tor API in order to register routes
$api->init();
// Handle routes
$routes->handle();
} catch (RouteNotFoundException $ex) {
$routes->request->status(404, "Route not found")->send(["error" => ["message" => $ex->getMessage()]]);
} catch (CallbackNotFound $ex) {
$routes->request->status(404, "Callback method not found")->send(["error" => ["message" => $ex->getMessage()]]);
} catch (Exception $ex) {
$code = $ex->getCode() ?? 500;
$routes->request->status($code)->send(["error" => ["message" => $ex->getMessage()]]);
}
?>