From 764fd1fe2f82581318fa183a4110866e5d3ceed5 Mon Sep 17 00:00:00 2001 From: Yvo Brevoort Date: Mon, 9 Nov 2020 14:32:09 +0100 Subject: [PATCH] prepare pubsub and base url --- src/Server.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Server.php b/src/Server.php index 15feb74..3e379df 100644 --- a/src/Server.php +++ b/src/Server.php @@ -37,6 +37,8 @@ class Server private $filesystem; /** @var Response */ private $response; + private $pubsub; + private $baseUrl; //////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ @@ -44,6 +46,8 @@ final public function __construct(Filesystem $filesystem, Response $response) { $this->filesystem = $filesystem; $this->response = $response; + $this->pubsub = ''; + $this->baseUrl = ''; } final public function respondToRequest(Request $request) : Response @@ -82,6 +86,9 @@ private function handle(string $method, string $path, $contents, $request) : Res $response = $this->response; $filesystem = $this->filesystem; + $this->pubsub = getenv('PUBSUB_URL') ?: ("http://" . $request->getServerParams()["SERVER_NAME"] . ":8080/"); + $this->baseUrl = "https://" . $request->getServerParams()["SERVER_NAME"]; + // Lets assume the worst... $response = $response->withStatus(500); @@ -108,8 +115,7 @@ private function handle(string $method, string $path, $contents, $request) : Res $response->getBody()->rewind(); $response->getBody()->write(''); // FIXME: pubsub info should be passed to this instead - $pubsub = getenv('PUBSUB_URL') ?: ("http://" . $request->getServerParams()["SERVER_NAME"] . ":8080/"); - $response = $response->withHeader("updates-via", $pubsub); + $response = $response->withHeader("updates-via", $this->pubsub); } break; @@ -303,12 +309,15 @@ private function handleCreateDirectoryRequest(Response $response, string $path) } private function sendWebsocketUpdate($path) { - $baseUrl = "https://" . $request->getServerParams()["SERVER_NAME"]; - $pubsub = getenv('PUBSUB_URL') ?: ("http://" . $request->getServerParams()["SERVER_NAME"] . ":8080/"); + $pubsub = $this->pubsub; $pubsub = str_replace("https://", "ws://", $pubsub); $pubsub = str_replace("http://", "ws://", $pubsub); + + $baseUrl = $this->baseUrl; + $client = new \WebSocket\Client($pubsub); $client->send("pub $baseUrl$path\n"); + while ($path != "/") { $path = $this->parentPath($path); $client->send("pub $baseUrl$path\n");