dnsservice/ansible/assets/api/http.php
2024-08-05 15:04:05 -05:00

48 lines
1016 B
PHP

<?php
include_once "lib/message.php";
$_routing_table = [];
$_msg = new Message();
$_msg = $_msg->withUri($_msg->getUri()->withPath(trim_prefix(
$_msg->getUri()->getPath(),
"/api")
));
function msg():Message {
global $_msg;
return $_msg;
}
function _route($path,$handler,$filter=null) {
global $_routing_table;
$_routing_table[] = [$path,$filter,$handler];
}
function _methodfn($method) {
return function($path, $handler) use ($method) {
_route(
$path,
$handler,
fn() => mb_strtolower(msg()->getMethod()) == mb_strtolower($method),
);
};
}
function dispatch() {
global $_routing_table;
foreach ($_routing_table as $value) {
if ((!is_null($value[1])) && (!$value[1]())) { continue; }
if (preg_match("|" . $value[0] . "|i",
msg()->getUri()->getPath(),
)) {
try {
$value[2]();
} catch (Exception $e) {
echo 'exceptional error: ', $e->getMessage(), "\n";
}
break;
}
http_response_code(404);
}
}
?>