mirror of
https://github.com/sigmasternchen/useful-api.org
synced 2025-03-15 07:58:55 +00:00
cleanup: Move renderer call to middleware
This commit is contained in:
parent
a2120cb99d
commit
9081b05a0e
6 changed files with 30 additions and 24 deletions
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
return function (array $context) {
|
||||
$context["renderer"]([
|
||||
return [
|
||||
"address" => $_SERVER['REMOTE_ADDR'],
|
||||
]);
|
||||
];
|
||||
};
|
||||
|
|
|
@ -1,12 +1,14 @@
|
|||
<?php
|
||||
|
||||
require_once(ROOT . "/utils/error.php");
|
||||
|
||||
return function (array $context) {
|
||||
if (key_exists("to", $_GET)) {
|
||||
$context["renderer"](idn_to_ascii($_GET["to"]));
|
||||
return idn_to_ascii($_GET["to"]);
|
||||
} elseif (key_exists("from", $_GET)) {
|
||||
$context["renderer"](idn_to_utf8($_GET["from"]));
|
||||
return idn_to_utf8($_GET["from"]);
|
||||
} else {
|
||||
setStatusCode(400);
|
||||
$context["renderer"](errorResponse("Unknown mode", "Please specify one of the following query parameters: to, from"));
|
||||
return errorResponse("Unknown mode", "Please specify one of the following query parameters: to, from");
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
return function (array $context) {
|
||||
$context["renderer"]([
|
||||
return [
|
||||
"value" => "Hello World!",
|
||||
]);
|
||||
];
|
||||
};
|
||||
|
|
|
@ -94,25 +94,22 @@ function whoisDomain(string $domain) {
|
|||
}
|
||||
|
||||
return function (array $context) {
|
||||
$result = null;
|
||||
|
||||
if (key_exists("ip", $_GET)) {
|
||||
$ip = $_GET["ip"];
|
||||
|
||||
if (!filter_var($ip, FILTER_VALIDATE_IP)) {
|
||||
setStatusCode(400);
|
||||
$result = errorResponse("Invalid IP address", "Please provide a valid IP address.");
|
||||
return errorResponse("Invalid IP address", "Please provide a valid IP address.");
|
||||
} else {
|
||||
$result = whoisIp($ip);
|
||||
return whoisIp($ip);
|
||||
}
|
||||
} elseif (key_exists("domain", $_GET)) {
|
||||
$domain = $_GET["domain"];
|
||||
|
||||
$result = whoisDomain($domain);
|
||||
return whoisDomain($domain);
|
||||
} else {
|
||||
setStatusCode(400);
|
||||
$result = errorResponse("Unknown mode", "Please specify one of the following query parameters: ip, domain");
|
||||
return errorResponse("Unknown mode", "Please specify one of the following query parameters: ip, domain");
|
||||
}
|
||||
|
||||
$context["renderer"]($result);
|
||||
};
|
||||
|
||||
|
|
|
@ -6,8 +6,14 @@ function useRenderer($handler, string $default = "JSON", string $query_param = "
|
|||
return function (array $context) use ($handler, $default, $query_param) {
|
||||
$rendererMap = require(ROOT . "/renderer/renderer.php");
|
||||
|
||||
$context["renderer"] = $rendererMap[strtoupper($_GET[$query_param] ?? "")] ?? $rendererMap[$default];
|
||||
$renderer = $rendererMap[strtoupper($_GET[$query_param] ?? "")] ?? $rendererMap[$default];
|
||||
$context["renderer"] = $renderer;
|
||||
|
||||
$handler($context);
|
||||
$result = $handler($context);
|
||||
if ($result !== null) {
|
||||
$renderer($result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
};
|
||||
}
|
|
@ -52,9 +52,7 @@ class Router {
|
|||
array_get_or_add($method, $this->routes, [])[$path] = $handler;
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
//var_dump($this->routes);
|
||||
|
||||
public function execute($context = []) {
|
||||
$path = $this->getPath($_SERVER["REQUEST_URI"]);
|
||||
$route = $this->findRoute($_SERVER["REQUEST_METHOD"], $path);
|
||||
|
||||
|
@ -62,11 +60,14 @@ class Router {
|
|||
$route = $this->notFoundHandler;
|
||||
}
|
||||
|
||||
$context = [
|
||||
"REQUEST_PATH" => $path,
|
||||
];
|
||||
$context["REQUEST_PATH"] = $path;
|
||||
|
||||
$route($context);
|
||||
return $route($context);
|
||||
}
|
||||
|
||||
// calling magic to make the router a handler and thus cascade-able
|
||||
public function __call(string $name, array $arguments) {
|
||||
return $this->execute($arguments[0] ?? []);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue