cleanup: Add router layer for APIs, made context a reference

This commit is contained in:
overflowerror 2023-11-25 13:23:29 +01:00
parent 8255421d0e
commit 9d676afe5b
9 changed files with 41 additions and 38 deletions

View file

@ -1,5 +1,5 @@
<?php
return function (array $context) {
return function (array &$context) {
echo "Hello World";
};

View file

@ -1,6 +1,6 @@
<?php
return function (array $context) {
return function (array &$context) {
return [
"address" => $_SERVER['REMOTE_ADDR'],
];

View file

@ -2,7 +2,7 @@
require_once(ROOT . "/utils/error.php");
return function (array $context) {
return function (array &$context) {
if (key_exists("to", $_GET)) {
return idn_to_ascii($_GET["to"]);
} elseif (key_exists("from", $_GET)) {

View file

@ -1,10 +1,15 @@
<?php
require_once(ROOT . "/router/Router.php");
require_once(ROOT . "/middleware/renderer.php");
require_once(ROOT . "/middleware/log.php");
function fromController(string $path) {
return function(array $context) use ($path) {
function fromController(string $path, string $endpoint = null) {
return function(array &$context) use ($path, $endpoint) {
if ($endpoint)
$context["endpoint"] = $endpoint;
return (require(ROOT . "/controllers/" . $path . ".php"))($context);
};
}
@ -13,29 +18,19 @@ return function(Router $router) {
$router->addRoute(GET, "/", fromController("/GET"));
$router->addRoute(GET, "/test", useRenderer(fromController("/test/GET")));
$router->addRoute(GET, "/ipaddress",
useLog(
useRenderer(
fromController("/ipaddress/GET")
),
"ipaddress"
)
);
$router->addRoute(GET, "/whois",
useLog(
useRenderer(
fromController("/whois/GET")
),
"whois"
)
$apiRouter = new Router("");
$router->addRoute(GET, "/.*",
useLog(useRenderer($apiRouter))
);
$router->addRoute(GET, "/punycode",
useLog(
useRenderer(
fromController("/punycode/GET")
),
"punycode"
)
$apiRouter->addRoute(GET, "/ipaddress",
fromController("/ipaddress/GET", "ipaddress")
);
$apiRouter->addRoute(GET, "/whois",
fromController("/whois/GET", "whois")
);
$apiRouter->addRoute(GET, "/punycode",
fromController("/punycode/GET", "punycode")
);
};

View file

@ -1,6 +1,6 @@
<?php
return function (array $context) {
return function (array &$context) {
return [
"value" => "Hello World!",
];

View file

@ -93,8 +93,9 @@ function whoisDomain(string $domain) {
];
}
return function (array $context) {
return function (array &$context) {
if (key_exists("ip", $_GET)) {
$context["endpoint"] = "whois/ip";
$ip = $_GET["ip"];
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
@ -104,6 +105,7 @@ return function (array $context) {
return whoisIp($ip);
}
} elseif (key_exists("domain", $_GET)) {
$context["endpoint"] = "whois/domain";
$domain = $_GET["domain"];
return whoisDomain($domain);

View file

@ -2,14 +2,16 @@
require_once(ROOT . "/persistence/models/LogEntry.php");
function useLog($handler, string $endpoint) {
return function (array $context) use ($handler, $endpoint) {
function useLog($handler, string $endpoint = "") {
return function (array &$context) use ($handler, $endpoint) {
$context["endpoint"] = $endpoint;
$result = $handler($context);
$accessKey = $context["ACCESS_KEY"] ?? "";
$entry = new LogEntry(
$endpoint,
$context["endpoint"],
$_SERVER['REMOTE_ADDR'],
$accessKey,
);

View file

@ -3,7 +3,7 @@
require_once(ROOT . "/utils/arrays.php");
function useRenderer($handler, string $default = "JSON", string $query_param = "format") {
return function (array $context) use ($handler, $default, $query_param) {
return function (array &$context) use ($handler, $default, $query_param) {
$rendererMap = require(ROOT . "/renderer/renderer.php");
$renderer = $rendererMap[strtoupper($_GET[$query_param] ?? "")] ?? $rendererMap[$default];

View file

@ -15,13 +15,15 @@ const PATCH = "PATCH";
class Router {
private $routes = [];
private $prefix = "";
public $notFoundHandler;
function __construct() {
$this->notFoundHandler = function($context) {
function __construct(string $prefix = "") {
$this->notFoundHandler = function(array &$context) {
setStatusCode(404);
require(ROOT . "/templates/404.php");
};
$this->prefix = $prefix;
}
private function findRoute(string $method, string $url) {
@ -52,8 +54,10 @@ class Router {
array_get_or_add($method, $this->routes, [])[$path] = $handler;
}
public function execute($context = []) {
public function execute(array &$context = []) {
$path = $this->getPath($_SERVER["REQUEST_URI"]);
$path = substr($path, strlen($this->prefix));
$route = $this->findRoute($_SERVER["REQUEST_METHOD"], $path);
if (!$route) {
@ -66,8 +70,8 @@ class Router {
}
// calling magic to make the router a handler and thus cascade-able
public function __call(string $name, array $arguments) {
return $this->execute($arguments[0] ?? []);
public function __invoke(array &$context = []) {
return $this->execute($context);
}
}