Moved api enabled logic from handler to server

This commit is contained in:
Chris 2024-03-14 17:13:53 +01:00
parent d8ae8ade70
commit 018c88e0b6
2 changed files with 29 additions and 65 deletions

View File

@ -32,37 +32,8 @@ class ApiHandler
return new Promise( return new Promise(
function (callable $resolve, callable $reject) use ($next, $request) { function (callable $resolve, callable $reject) use ($next, $request) {
if (!$this->config->get('server.enable_api', true)) {
$resolve($next($request));
return;
}
$path = $request->getUri()->getPath(); $path = $request->getUri()->getPath();
// FIXME remove this when done debugging
if ($path === "/index.html") {
$resolve(Response::html(
<<<ENDHTML
<html>
<head>
</head>
<body>
<script type="text/javascript">
const events = new EventSource("http://127.0.0.1:9000/.well-known/mercure?topic=https://example.com/books/1");
events.onmessage = function (msg) {
console.log(msg);
const message = document.createElement('div');
message.innerText = msg.data;
document.getElementById('messages').appendChild(message);
};
</script>
<div id="messages">
</body>
</html>
ENDHTML
));
}
switch (true) { switch (true) {
case preg_match('<^/.well-known/mercure/subscriptions(/.+?){0,1}$>', $path, $m): case preg_match('<^/.well-known/mercure/subscriptions(/.+?){0,1}$>', $path, $m):
$query = explode("/", trim($m[1]??null, "/")); $query = explode("/", trim($m[1]??null, "/"));

View File

@ -29,7 +29,7 @@ class Server
private LoopInterface $loop; private LoopInterface $loop;
private HttpServer $server; private ?HttpServer $server = null;
private SplObjectStorage $webSocketClients; private SplObjectStorage $webSocketClients;
@ -37,14 +37,6 @@ class Server
private Logger $logger; private Logger $logger;
private ResponseMiddleware $responseMiddleware;
private SecurityMiddleware $securityMiddleware;
private WebSocketHandler $webSocketHandler;
private MercureHandler $mercureHandler;
private ApiHandler $apiRequestHandler;
private NotFoundHandler $notFoundHandler;
/** /**
* *
* *
@ -68,8 +60,6 @@ class Server
}); });
$this->webSocketClients = new SplObjectStorage(); $this->webSocketClients = new SplObjectStorage();
$this->server = $this->createHttpServer();
} }
/** /**
@ -79,6 +69,10 @@ class Server
*/ */
public function listen(ServerInterface $socket): void public function listen(ServerInterface $socket): void
{ {
if (!$this->server) {
$this->server = $this->createHttpServer();
}
$this->server->listen($socket); $this->server->listen($socket);
$this->logger->info(sprintf( $this->logger->info(sprintf(
"Listening on %s", "Listening on %s",
@ -100,38 +94,37 @@ class Server
$stack[] = new LimitConcurrentRequestsMiddleware($maxConcurrent); $stack[] = new LimitConcurrentRequestsMiddleware($maxConcurrent);
$stack[] = new RequestBodyBufferMiddleware($maxRequestBody); $stack[] = new RequestBodyBufferMiddleware($maxRequestBody);
$stack = [ ...$stack, $stack[] = new ResponseMiddleware(
$this->responseMiddleware = new ResponseMiddleware( config: $this->config,
config: $this->config, logger: $this->logger->withName("http"),
logger: $this->logger->withName("http"), );
), $stack[] = new SecurityMiddleware(
$this->securityMiddleware = new SecurityMiddleware( config: $this->config
config: $this->config );
),
];
if ($this->config->getEnableWebSockets()) { if ($this->config->getEnableWebSockets()) {
$stack = [ ...$stack, $stack[] = $webSocketHandler = new WebSocketHandler(
$this->webSocketHandler = new WebSocketHandler( config: $this->config,
config: $this->config, webSocketClients: $this->webSocketClients,
webSocketClients: $this->webSocketClients, topicManager: $this->topicManager
topicManager: $this->topicManager );
),
];
$this->logger->warning("The WebSocket support is incomplete and insecure, but enabling it as requested."); $this->logger->warning("The WebSocket support is incomplete and insecure, but enabling it as requested.");
} }
$stack = [ ...$stack, $stack[] = new MercureHandler(
$this->mercureHandler = new MercureHandler( config: $this->config,
topicManager: $this->topicManager
);
if ($this->config->get('server.enable_api', true)) {
$stack[] = new ApiHandler(
config: $this->config, config: $this->config,
topicManager: $this->topicManager topicManager: $this->topicManager
), );
$this->apiRequestHandler = new ApiHandler( $this->logger->info("Enabling the API middleware");
config: $this->config, }
topicManager: $this->topicManager
), $stack[] = new NotFoundHandler();
$this->notFoundHandler = new NotFoundHandler()
];
return new HttpServer(...$stack); return new HttpServer(...$stack);
} }