Moved api enabled logic from handler to server

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

View File

@ -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(
config: $this->config,
logger: $this->logger->withName("http"),
),
$this->securityMiddleware = new SecurityMiddleware(
config: $this->config
),
];
$stack[] = new ResponseMiddleware(
config: $this->config,
logger: $this->logger->withName("http"),
);
$stack[] = new SecurityMiddleware(
config: $this->config
);
if ($this->config->getEnableWebSockets()) {
$stack = [ ...$stack,
$this->webSocketHandler = new WebSocketHandler(
config: $this->config,
webSocketClients: $this->webSocketClients,
topicManager: $this->topicManager
),
];
$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
);
if ($this->config->get('server.enable_api', true)) {
$stack[] = new ApiHandler(
config: $this->config,
topicManager: $this->topicManager
),
$this->apiRequestHandler = 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);
}