From e10fd6c471baa353c00a5bed4f55f2e6d40e5bea Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Tue, 12 Mar 2024 02:21:42 +0100 Subject: [PATCH] Refactoring, logging improvements --- bin/mercureactd | 15 ++++++++++++--- src/Broker/SubscriberInterface.php | 2 ++ src/Broker/TopicManager.php | 9 +++++++-- src/Daemon.php | 9 +++++---- src/{Http => }/Exception/RequestException.php | 2 +- src/{Http => }/Exception/SecurityException.php | 2 +- src/Http/Middleware/MercureHandler.php | 4 ++-- src/Http/Middleware/ResponseMiddleware.php | 2 +- src/Http/Middleware/SecurityMiddleware.php | 2 +- src/Http/Server.php | 7 ++++++- 10 files changed, 38 insertions(+), 16 deletions(-) rename src/{Http => }/Exception/RequestException.php (79%) rename src/{Http => }/Exception/SecurityException.php (75%) diff --git a/bin/mercureactd b/bin/mercureactd index caac44c..b22fee3 100755 --- a/bin/mercureactd +++ b/bin/mercureactd @@ -9,14 +9,14 @@ file_exists(__DIR__."/../../../autoload.php") && require_once __DIR__."/../../.. if (file_exists(__DIR__."/../src/meta")) { $meta = require_once(__DIR__."/../src/meta"); - define("MERCUREACT_VERSION", $meta['version']??'0.0.0'); + define("MERCUREACT_VERSION", $meta['version']?:'0.0.0'); define("MERCUREACT_BUILDTIME", $meta['buildtime']); } else { define("MERCUREACT_VERSION", "DEV"); define("MERCUREACT_BUILDTIME", null); } -$opts = getopt("c:C:h"); +$opts = getopt("c:C:hvV"); if (isset($opts['h'])) { $info = "v".MERCUREACT_VERSION.(MERCUREACT_BUILDTIME?("\nBuilt on ".MERCUREACT_BUILDTIME):""); @@ -26,6 +26,8 @@ if (isset($opts['h'])) { Options: + -V Print version and exit + -v Verbose debug logging -c config Read configuration from file -C config Write a new configuration to file and open with editor @@ -34,6 +36,11 @@ if (isset($opts['h'])) { exit(0); } +if (isset($opts['V'])) { + fprintf(STDOUT, "Mercureact %s\nBuilt on %s\n", MERCUREACT_VERSION, MERCUREACT_BUILDTIME); + exit(0); +} + if (isset($opts['C'])) { $file = $opts['C']; if (file_exists($file)) { @@ -78,5 +85,7 @@ if (isset($opts['c'])) { ->setJwtSecret("!ChangeThisMercureHubJWTSecretKey!"); } -$daemon = new Daemon($config); +$verbose = isset($opts['v']); + +$daemon = new Daemon($config, $verbose); $daemon->start(); diff --git a/src/Broker/SubscriberInterface.php b/src/Broker/SubscriberInterface.php index 5f24d59..4dd9d97 100644 --- a/src/Broker/SubscriberInterface.php +++ b/src/Broker/SubscriberInterface.php @@ -9,4 +9,6 @@ interface SubscriberInterface public function isAuthorized(): bool; public function getPayload(): ?array; + + public function getId(): string; } \ No newline at end of file diff --git a/src/Broker/TopicManager.php b/src/Broker/TopicManager.php index fefd29d..f99ec34 100644 --- a/src/Broker/TopicManager.php +++ b/src/Broker/TopicManager.php @@ -2,6 +2,7 @@ namespace NoccyLabs\Mercureact\Broker; +use Psr\Log\LoggerInterface; use SplObjectStorage; class TopicManager @@ -13,7 +14,7 @@ class TopicManager private SplObjectStorage $subscribers; - public function __construct() + public function __construct(private LoggerInterface $logger) { $this->subscribers = new SplObjectStorage(); } @@ -21,6 +22,7 @@ class TopicManager public function getTopic(string $topic): Topic { if (!isset($this->topics[$topic])) { + $this->logger->debug("Created topic: {$topic}"); $this->topics[$topic] = new Topic($topic); } return $this->topics[$topic]; @@ -30,12 +32,14 @@ class TopicManager { $this->lastEventId = $message->id; foreach ($message->topic as $topic) { + $this->logger->debug("Publish: {$message->id} → ".json_encode($message->topic,JSON_UNESCAPED_SLASHES)); $this->getTopic($topic)->publish($message); } } public function subscribe(SubscriberInterface $subscriber, array $topics): void { + $this->logger->debug("Subscribed: ".$subscriber->getId()." + ".json_encode($topics,JSON_UNESCAPED_SLASHES)); $this->subscribers->attach($subscriber); foreach ($topics as $topic) { $this->getTopic($topic)->addSubscriber($subscriber); @@ -44,8 +48,9 @@ class TopicManager public function unsubscribe(SubscriberInterface $subscriber, ?array $topics=null): void { - $this->subscribers->detach($subscriber); + $this->logger->debug("Unsubscribed: ".$subscriber->getId()." - ".json_encode($topics,JSON_UNESCAPED_SLASHES)); if (!$topics) { + $this->subscribers->detach($subscriber); foreach ($this->topics as $topic) { $topic->removeSubscriber($subscriber); } diff --git a/src/Daemon.php b/src/Daemon.php index b2d911b..0d00ba4 100644 --- a/src/Daemon.php +++ b/src/Daemon.php @@ -3,6 +3,7 @@ namespace NoccyLabs\Mercureact; use Monolog\Handler\StreamHandler; +use Monolog\Level; use Monolog\Logger; use NoccyLabs\Mercureact\Http\Server; use Psr\Log\LoggerInterface; @@ -20,17 +21,17 @@ class Daemon private LoggerInterface $logger; - public function __construct(Configuration $config, ?LoopInterface $loop=null) + public function __construct(Configuration $config, bool $verbose=false, ?LoopInterface $loop=null) { $this->config = $config; $this->loop = $loop??Loop::get(); - $this->logger = $this->createLogger(); + $this->logger = $this->createLogger($verbose); } - private function createLogger(): Logger + private function createLogger(bool $verbose): Logger { $handlers = [ - new StreamHandler(STDOUT) + new StreamHandler(STDOUT, $verbose?Level::Debug:Level::Info) ]; $logger = new Logger("main", $handlers); return $logger; diff --git a/src/Http/Exception/RequestException.php b/src/Exception/RequestException.php similarity index 79% rename from src/Http/Exception/RequestException.php rename to src/Exception/RequestException.php index 0037f06..6626b9e 100644 --- a/src/Http/Exception/RequestException.php +++ b/src/Exception/RequestException.php @@ -1,6 +1,6 @@ config = $config; $this->logger = $logger ?? new NullLogger(); + if ($logger instanceof Logger) { + $topicLogger = $logger->withName("broker"); + } else { + $topicLogger = $this->logger; + } - $this->topicManager = new TopicManager(); + $this->topicManager = new TopicManager($topicLogger); $this->loop->addPeriodicTimer(30, function () { $this->topicManager->garbageCollect(); });