Skip to content

Commit

Permalink
prepare pubsub and base url
Browse files Browse the repository at this point in the history
  • Loading branch information
ylebre committed Nov 9, 2020
1 parent f3862bb commit 764fd1f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ class Server
private $filesystem;
/** @var Response */
private $response;
private $pubsub;
private $baseUrl;

//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

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
Expand Down Expand Up @@ -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);

Expand All @@ -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;

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 764fd1f

Please sign in to comment.