Moved api enabled logic from handler to server
This commit is contained in:
parent
d8ae8ade70
commit
018c88e0b6
@ -32,37 +32,8 @@ class ApiHandler
|
||||
return new Promise(
|
||||
function (callable $resolve, callable $reject) use ($next, $request) {
|
||||
|
||||
if (!$this->config->get('server.enable_api', true)) {
|
||||
$resolve($next($request));
|
||||
return;
|
||||
}
|
||||
|
||||
$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) {
|
||||
case preg_match('<^/.well-known/mercure/subscriptions(/.+?){0,1}$>', $path, $m):
|
||||
$query = explode("/", trim($m[1]??null, "/"));
|
||||
|
@ -29,7 +29,7 @@ class Server
|
||||
|
||||
private LoopInterface $loop;
|
||||
|
||||
private HttpServer $server;
|
||||
private ?HttpServer $server = null;
|
||||
|
||||
private SplObjectStorage $webSocketClients;
|
||||
|
||||
@ -37,14 +37,6 @@ class Server
|
||||
|
||||
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->server = $this->createHttpServer();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,6 +69,10 @@ class Server
|
||||
*/
|
||||
public function listen(ServerInterface $socket): void
|
||||
{
|
||||
if (!$this->server) {
|
||||
$this->server = $this->createHttpServer();
|
||||
}
|
||||
|
||||
$this->server->listen($socket);
|
||||
$this->logger->info(sprintf(
|
||||
"Listening on %s",
|
||||
@ -100,38 +94,37 @@ class Server
|
||||
$stack[] = new LimitConcurrentRequestsMiddleware($maxConcurrent);
|
||||
$stack[] = new RequestBodyBufferMiddleware($maxRequestBody);
|
||||
|
||||
$stack = [ ...$stack,
|
||||
$this->responseMiddleware = new ResponseMiddleware(
|
||||
$stack[] = new ResponseMiddleware(
|
||||
config: $this->config,
|
||||
logger: $this->logger->withName("http"),
|
||||
),
|
||||
$this->securityMiddleware = new SecurityMiddleware(
|
||||
);
|
||||
$stack[] = new SecurityMiddleware(
|
||||
config: $this->config
|
||||
),
|
||||
];
|
||||
);
|
||||
|
||||
if ($this->config->getEnableWebSockets()) {
|
||||
$stack = [ ...$stack,
|
||||
$this->webSocketHandler = new WebSocketHandler(
|
||||
$stack[] = $webSocketHandler = new WebSocketHandler(
|
||||
config: $this->config,
|
||||
webSocketClients: $this->webSocketClients,
|
||||
topicManager: $this->topicManager
|
||||
),
|
||||
];
|
||||
);
|
||||
$this->logger->warning("The WebSocket support is incomplete and insecure, but enabling it as requested.");
|
||||
}
|
||||
|
||||
$stack = [ ...$stack,
|
||||
$this->mercureHandler = new MercureHandler(
|
||||
$stack[] = new MercureHandler(
|
||||
config: $this->config,
|
||||
topicManager: $this->topicManager
|
||||
),
|
||||
$this->apiRequestHandler = new ApiHandler(
|
||||
);
|
||||
|
||||
if ($this->config->get('server.enable_api', true)) {
|
||||
$stack[] = new ApiHandler(
|
||||
config: $this->config,
|
||||
topicManager: $this->topicManager
|
||||
),
|
||||
$this->notFoundHandler = new NotFoundHandler()
|
||||
];
|
||||
);
|
||||
$this->logger->info("Enabling the API middleware");
|
||||
}
|
||||
|
||||
$stack[] = new NotFoundHandler();
|
||||
|
||||
return new HttpServer(...$stack);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user