mirror of
https://github.com/sigmasternchen/useful-api.org
synced 2025-03-15 07:58:55 +00:00
cleanup: Add router layer for APIs, made context a reference
This commit is contained in:
parent
8255421d0e
commit
9d676afe5b
9 changed files with 41 additions and 38 deletions
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
return function (array $context) {
|
return function (array &$context) {
|
||||||
echo "Hello World";
|
echo "Hello World";
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
return function (array $context) {
|
return function (array &$context) {
|
||||||
return [
|
return [
|
||||||
"address" => $_SERVER['REMOTE_ADDR'],
|
"address" => $_SERVER['REMOTE_ADDR'],
|
||||||
];
|
];
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
require_once(ROOT . "/utils/error.php");
|
require_once(ROOT . "/utils/error.php");
|
||||||
|
|
||||||
return function (array $context) {
|
return function (array &$context) {
|
||||||
if (key_exists("to", $_GET)) {
|
if (key_exists("to", $_GET)) {
|
||||||
return idn_to_ascii($_GET["to"]);
|
return idn_to_ascii($_GET["to"]);
|
||||||
} elseif (key_exists("from", $_GET)) {
|
} elseif (key_exists("from", $_GET)) {
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once(ROOT . "/router/Router.php");
|
||||||
|
|
||||||
require_once(ROOT . "/middleware/renderer.php");
|
require_once(ROOT . "/middleware/renderer.php");
|
||||||
require_once(ROOT . "/middleware/log.php");
|
require_once(ROOT . "/middleware/log.php");
|
||||||
|
|
||||||
function fromController(string $path) {
|
function fromController(string $path, string $endpoint = null) {
|
||||||
return function(array $context) use ($path) {
|
return function(array &$context) use ($path, $endpoint) {
|
||||||
|
if ($endpoint)
|
||||||
|
$context["endpoint"] = $endpoint;
|
||||||
|
|
||||||
return (require(ROOT . "/controllers/" . $path . ".php"))($context);
|
return (require(ROOT . "/controllers/" . $path . ".php"))($context);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -13,29 +18,19 @@ return function(Router $router) {
|
||||||
$router->addRoute(GET, "/", fromController("/GET"));
|
$router->addRoute(GET, "/", fromController("/GET"));
|
||||||
$router->addRoute(GET, "/test", useRenderer(fromController("/test/GET")));
|
$router->addRoute(GET, "/test", useRenderer(fromController("/test/GET")));
|
||||||
|
|
||||||
$router->addRoute(GET, "/ipaddress",
|
$apiRouter = new Router("");
|
||||||
useLog(
|
$router->addRoute(GET, "/.*",
|
||||||
useRenderer(
|
useLog(useRenderer($apiRouter))
|
||||||
fromController("/ipaddress/GET")
|
|
||||||
),
|
|
||||||
"ipaddress"
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$router->addRoute(GET, "/whois",
|
|
||||||
useLog(
|
|
||||||
useRenderer(
|
|
||||||
fromController("/whois/GET")
|
|
||||||
),
|
|
||||||
"whois"
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$router->addRoute(GET, "/punycode",
|
$apiRouter->addRoute(GET, "/ipaddress",
|
||||||
useLog(
|
fromController("/ipaddress/GET", "ipaddress")
|
||||||
useRenderer(
|
);
|
||||||
fromController("/punycode/GET")
|
$apiRouter->addRoute(GET, "/whois",
|
||||||
),
|
fromController("/whois/GET", "whois")
|
||||||
"punycode"
|
);
|
||||||
)
|
|
||||||
|
$apiRouter->addRoute(GET, "/punycode",
|
||||||
|
fromController("/punycode/GET", "punycode")
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
return function (array $context) {
|
return function (array &$context) {
|
||||||
return [
|
return [
|
||||||
"value" => "Hello World!",
|
"value" => "Hello World!",
|
||||||
];
|
];
|
||||||
|
|
|
@ -93,8 +93,9 @@ function whoisDomain(string $domain) {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return function (array $context) {
|
return function (array &$context) {
|
||||||
if (key_exists("ip", $_GET)) {
|
if (key_exists("ip", $_GET)) {
|
||||||
|
$context["endpoint"] = "whois/ip";
|
||||||
$ip = $_GET["ip"];
|
$ip = $_GET["ip"];
|
||||||
|
|
||||||
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
|
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
|
||||||
|
@ -104,6 +105,7 @@ return function (array $context) {
|
||||||
return whoisIp($ip);
|
return whoisIp($ip);
|
||||||
}
|
}
|
||||||
} elseif (key_exists("domain", $_GET)) {
|
} elseif (key_exists("domain", $_GET)) {
|
||||||
|
$context["endpoint"] = "whois/domain";
|
||||||
$domain = $_GET["domain"];
|
$domain = $_GET["domain"];
|
||||||
|
|
||||||
return whoisDomain($domain);
|
return whoisDomain($domain);
|
||||||
|
|
|
@ -2,14 +2,16 @@
|
||||||
|
|
||||||
require_once(ROOT . "/persistence/models/LogEntry.php");
|
require_once(ROOT . "/persistence/models/LogEntry.php");
|
||||||
|
|
||||||
function useLog($handler, string $endpoint) {
|
function useLog($handler, string $endpoint = "") {
|
||||||
return function (array $context) use ($handler, $endpoint) {
|
return function (array &$context) use ($handler, $endpoint) {
|
||||||
|
$context["endpoint"] = $endpoint;
|
||||||
|
|
||||||
$result = $handler($context);
|
$result = $handler($context);
|
||||||
|
|
||||||
$accessKey = $context["ACCESS_KEY"] ?? "";
|
$accessKey = $context["ACCESS_KEY"] ?? "";
|
||||||
|
|
||||||
$entry = new LogEntry(
|
$entry = new LogEntry(
|
||||||
$endpoint,
|
$context["endpoint"],
|
||||||
$_SERVER['REMOTE_ADDR'],
|
$_SERVER['REMOTE_ADDR'],
|
||||||
$accessKey,
|
$accessKey,
|
||||||
);
|
);
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
require_once(ROOT . "/utils/arrays.php");
|
require_once(ROOT . "/utils/arrays.php");
|
||||||
|
|
||||||
function useRenderer($handler, string $default = "JSON", string $query_param = "format") {
|
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");
|
$rendererMap = require(ROOT . "/renderer/renderer.php");
|
||||||
|
|
||||||
$renderer = $rendererMap[strtoupper($_GET[$query_param] ?? "")] ?? $rendererMap[$default];
|
$renderer = $rendererMap[strtoupper($_GET[$query_param] ?? "")] ?? $rendererMap[$default];
|
||||||
|
|
|
@ -15,13 +15,15 @@ const PATCH = "PATCH";
|
||||||
|
|
||||||
class Router {
|
class Router {
|
||||||
private $routes = [];
|
private $routes = [];
|
||||||
|
private $prefix = "";
|
||||||
public $notFoundHandler;
|
public $notFoundHandler;
|
||||||
|
|
||||||
function __construct() {
|
function __construct(string $prefix = "") {
|
||||||
$this->notFoundHandler = function($context) {
|
$this->notFoundHandler = function(array &$context) {
|
||||||
setStatusCode(404);
|
setStatusCode(404);
|
||||||
require(ROOT . "/templates/404.php");
|
require(ROOT . "/templates/404.php");
|
||||||
};
|
};
|
||||||
|
$this->prefix = $prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function findRoute(string $method, string $url) {
|
private function findRoute(string $method, string $url) {
|
||||||
|
@ -52,8 +54,10 @@ class Router {
|
||||||
array_get_or_add($method, $this->routes, [])[$path] = $handler;
|
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 = $this->getPath($_SERVER["REQUEST_URI"]);
|
||||||
|
$path = substr($path, strlen($this->prefix));
|
||||||
|
|
||||||
$route = $this->findRoute($_SERVER["REQUEST_METHOD"], $path);
|
$route = $this->findRoute($_SERVER["REQUEST_METHOD"], $path);
|
||||||
|
|
||||||
if (!$route) {
|
if (!$route) {
|
||||||
|
@ -66,8 +70,8 @@ class Router {
|
||||||
}
|
}
|
||||||
|
|
||||||
// calling magic to make the router a handler and thus cascade-able
|
// calling magic to make the router a handler and thus cascade-able
|
||||||
public function __call(string $name, array $arguments) {
|
public function __invoke(array &$context = []) {
|
||||||
return $this->execute($arguments[0] ?? []);
|
return $this->execute($context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue