Configuration fixes, makefile tweaks

* The PHAR now gets tagged with version and buildtime
* WebSocket support can now be disabled
This commit is contained in:
2024-03-12 01:13:19 +01:00
parent da450b510a
commit b3476881e1
7 changed files with 77 additions and 33 deletions

View File

@ -13,9 +13,11 @@ use NoccyLabs\Mercureact\Http\Middleware\ResponseMiddleware;
use NoccyLabs\Mercureact\Http\Middleware\SecurityMiddleware;
use NoccyLabs\Mercureact\Http\Middleware\WebSocketHandler;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\Http\HttpServer;
use React\Socket\SecureServer;
use React\Socket\ServerInterface;
use SplObjectStorage;
@ -45,13 +47,13 @@ class Server
*
*
*/
public function __construct(Configuration $config, ?LoopInterface $loop=null)
public function __construct(Configuration $config, ?LoggerInterface $logger, ?LoopInterface $loop=null)
{
$this->loop = $loop??Loop::get();
$this->config = $config;
$this->logger = $this->createLogger();
$this->logger = $logger ?? new NullLogger();
$this->topicManager = new TopicManager();
$this->loop->addPeriodicTimer(30, function () {
@ -70,17 +72,11 @@ class Server
*/
public function listen(ServerInterface $socket): void
{
$this->logger->info("Listening on ".$socket->getAddress()."\n");
$this->server->listen($socket);
}
private function createLogger(): Logger
{
$handlers = [
new StreamHandler(STDOUT)
];
$logger = new Logger("main", $handlers);
return $logger;
$this->logger->info(sprintf(
"Listening on %s",
str_replace("tcp://",($socket instanceof SecureServer?"https://":"http://"),$socket->getAddress())
));
}
/**
@ -89,7 +85,7 @@ class Server
*/
private function createHttpServer(): HttpServer
{
return new HttpServer(
$stack = [
$this->responseMiddleware = new ResponseMiddleware(
config: $this->config,
logger: $this->logger->withName("http"),
@ -97,11 +93,18 @@ class Server
$this->securityMiddleware = new SecurityMiddleware(
config: $this->config
),
$this->webSocketHandler = new WebSocketHandler(
config: $this->config,
webSocketClients: $this->webSocketClients,
topicManager: $this->topicManager
),
];
if ($this->config->getEnableWebSockets()) {
$stack = [ ...$stack,
$this->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(
config: $this->config,
topicManager: $this->topicManager
@ -111,7 +114,9 @@ class Server
topicManager: $this->topicManager
),
$this->notFoundHandler = new NotFoundHandler()
);
];
return new HttpServer(...$stack);
}