Extracted middleware and handlers
This commit is contained in:
110
src/Http/Middleware/ApiHandler.php
Normal file
110
src/Http/Middleware/ApiHandler.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\Mercureact\Http\Middleware;
|
||||
|
||||
use NoccyLabs\Mercureact\Broker\TopicManager;
|
||||
use NoccyLabs\Mercureact\Configuration;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use React\Http\Message\Response;
|
||||
use React\Promise\Promise;
|
||||
use React\Promise\PromiseInterface;
|
||||
|
||||
class ApiHandler
|
||||
{
|
||||
|
||||
public static string $indexPage;
|
||||
|
||||
public function __construct(
|
||||
private Configuration $config,
|
||||
private TopicManager $topicManager
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param ServerRequestInterface $request
|
||||
* @param callable $next
|
||||
* @return PromiseInterface
|
||||
*/
|
||||
public function __invoke(ServerRequestInterface $request, callable $next): PromiseInterface
|
||||
{
|
||||
return new Promise(
|
||||
function (callable $resolve, callable $reject) use ($next, $request) {
|
||||
|
||||
$path = $request->getUri()->getPath();
|
||||
|
||||
if ($path === "/index.html") {
|
||||
$resolve(Response::html(self::$indexPage));
|
||||
}
|
||||
|
||||
switch (true) {
|
||||
case preg_match('<^/.well-known/mercure/subscriptions(/.+?)$>', $path, $m):
|
||||
$query = explode("/", trim($m[1]??null, "/"));
|
||||
$topic = array_shift($query);
|
||||
$subscription = array_shift($query);
|
||||
$resolve($this->apiGetSubscriptions($topic, $subscription));
|
||||
return;
|
||||
|
||||
case preg_match('<^/.well-known/mercureact/status$>', $path):
|
||||
$resolve([
|
||||
'server' => 'Mercureact/1.0',
|
||||
'topics' => $this->topicManager->getTopicCount(),
|
||||
'subscriptions' => $this->topicManager->getSubscriberCount(),
|
||||
'memoryPeak' => memory_get_peak_usage(true),
|
||||
'memoryUsage' => memory_get_usage(true)
|
||||
]);
|
||||
return;
|
||||
|
||||
case preg_match('<^/.well-known/mercureact/status$>', $path):
|
||||
$resolve([ 'version' => '1.0' ]);
|
||||
return;
|
||||
}
|
||||
|
||||
$resolve($next($request));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
private function apiGetSubscriptions(string|null $topic, string|null $subscription): ResponseInterface
|
||||
{
|
||||
// TODO implement once we can enumerate topics and subscriptions
|
||||
|
||||
// mock data
|
||||
$lastEventId = "urn:uuid:5e94c686-2c0b-4f9b-958c-92ccc3bbb4eb";
|
||||
$data = [
|
||||
"@context" => "https://mercure.rocks/",
|
||||
"id" => "/.well-known/mercure/subscriptions",
|
||||
"type" => "Subscriptions",
|
||||
"lastEventID" => $lastEventId,
|
||||
"subscriptions" => []
|
||||
];
|
||||
|
||||
return Response::json($data)
|
||||
->withHeader('Content-Type', 'application/ld+json')
|
||||
->withHeader('ETag', $lastEventId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
ApiHandler::$indexPage = <<<ENDHTML
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'self' http: 'unsafe-eval' 'unsafe-inline'; style-src 'self';">
|
||||
</head>
|
||||
<body>
|
||||
<script type="text/javascript">
|
||||
const events = new EventSource("http://127.0.0.1:9000/.well-known/mercure");
|
||||
events.onmessage = msg => console.log(msg);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
ENDHTML;
|
Reference in New Issue
Block a user