From 9d676afe5b9a3934834affc2b618f6f1540f7f19 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Sat, 25 Nov 2023 13:23:29 +0100 Subject: [PATCH] cleanup: Add router layer for APIs, made context a reference --- controllers/GET.php | 2 +- controllers/ipaddress/GET.php | 2 +- controllers/punycode/GET.php | 2 +- controllers/routes.php | 43 ++++++++++++++++------------------- controllers/test/GET.php | 2 +- controllers/whois/GET.php | 4 +++- middleware/log.php | 8 ++++--- middleware/renderer.php | 2 +- router/Router.php | 14 ++++++++---- 9 files changed, 41 insertions(+), 38 deletions(-) diff --git a/controllers/GET.php b/controllers/GET.php index 39ad8dc..e488878 100644 --- a/controllers/GET.php +++ b/controllers/GET.php @@ -1,5 +1,5 @@ $_SERVER['REMOTE_ADDR'], ]; diff --git a/controllers/punycode/GET.php b/controllers/punycode/GET.php index 2b728de..d141450 100644 --- a/controllers/punycode/GET.php +++ b/controllers/punycode/GET.php @@ -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)) { diff --git a/controllers/routes.php b/controllers/routes.php index c967076..fc4df12 100644 --- a/controllers/routes.php +++ b/controllers/routes.php @@ -1,10 +1,15 @@ 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") ); }; diff --git a/controllers/test/GET.php b/controllers/test/GET.php index cb6beca..f18f05e 100644 --- a/controllers/test/GET.php +++ b/controllers/test/GET.php @@ -1,6 +1,6 @@ "Hello World!", ]; diff --git a/controllers/whois/GET.php b/controllers/whois/GET.php index d5fecc5..6633b5a 100644 --- a/controllers/whois/GET.php +++ b/controllers/whois/GET.php @@ -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); diff --git a/middleware/log.php b/middleware/log.php index 295e5fd..0f9aac8 100644 --- a/middleware/log.php +++ b/middleware/log.php @@ -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, ); diff --git a/middleware/renderer.php b/middleware/renderer.php index 5be1ff1..a583d37 100644 --- a/middleware/renderer.php +++ b/middleware/renderer.php @@ -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]; diff --git a/router/Router.php b/router/Router.php index be22688..9c73126 100644 --- a/router/Router.php +++ b/router/Router.php @@ -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); } }