From f8464d2b4da91b3705d15a42d5617c03d039e6b8 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Mon, 4 May 2020 16:00:13 +0200 Subject: [PATCH] wip --- app/Client/Client.php | 34 +++++++++++++++--- config/expose.php | 4 +-- resources/views/client/dashboard.twig | 51 +++++++++++++++++++++------ tests/Feature/Server/AdminTest.php | 10 +++--- 4 files changed, 78 insertions(+), 21 deletions(-) diff --git a/app/Client/Client.php b/app/Client/Client.php index a08fcb4..b509911 100644 --- a/app/Client/Client.php +++ b/app/Client/Client.php @@ -15,6 +15,8 @@ use function Ratchet\Client\connect; class Client { + protected const MAX_CONNECTION_RETRIES = 3; + /** @var LoopInterface */ protected $loop; @@ -24,6 +26,9 @@ class Client /** @var CliRequestLogger */ protected $logger; + /** @var int */ + protected $connectionRetries = 0; + /** @var int */ protected $timeConnected = 0; @@ -72,15 +77,17 @@ class Client connect($wsProtocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control?authToken={$authToken}", [], [ 'X-Expose-Control' => 'enabled', ], $this->loop) - ->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain, $deferred) { + ->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain, $deferred, $authToken) { + $this->connectionRetries = 0; + $connection = ControlConnection::create($clientConnection); $connection->authenticate($sharedUrl, $subdomain); - $clientConnection->on('close', function() use ($deferred) { + $clientConnection->on('close', function() use ($deferred, $sharedUrl, $subdomain, $authToken) { $this->logger->error('Connection to server closed.'); - $this->exit($deferred); + $this->retryConnectionOrExit($sharedUrl, $subdomain, $authToken); }); $connection->on('authenticationFailed', function ($data) use ($deferred) { @@ -124,7 +131,11 @@ class Client $deferred->resolve(); }); - }, function (\Exception $e) use ($deferred) { + }, function (\Exception $e) use ($deferred, $sharedUrl, $subdomain, $authToken) { + if ($this->connectionRetries > 0) { + $this->retryConnectionOrExit($sharedUrl, $subdomain, $authToken); + return; + } $this->logger->error("Could not connect to the server."); $this->logger->error($e->getMessage()); @@ -142,4 +153,19 @@ class Client exit(1); }); } + + protected function retryConnectionOrExit(string $sharedUrl, $subdomain, $authToken = '') + { + $this->connectionRetries++; + + if ($this->connectionRetries <= static::MAX_CONNECTION_RETRIES) { + $this->loop->addTimer($this->connectionRetries, function() use ($sharedUrl, $subdomain, $authToken) { + $this->logger->info("Retrying connection ({$this->connectionRetries}/".static::MAX_CONNECTION_RETRIES.")"); + + $this->connectToServer($sharedUrl, $subdomain, $authToken); + }); + } else { + exit(1); + } + } } diff --git a/config/expose.php b/config/expose.php index dd59497..9d34956 100644 --- a/config/expose.php +++ b/config/expose.php @@ -1,8 +1,8 @@ 'localhost', - 'port' => 8080, + 'host' => 'expose.dev', + 'port' => 443, 'auth_token' => '', 'admin' => [ diff --git a/resources/views/client/dashboard.twig b/resources/views/client/dashboard.twig index f49b405..0566a4a 100644 --- a/resources/views/client/dashboard.twig +++ b/resources/views/client/dashboard.twig @@ -100,8 +100,8 @@ -
-
+
+
@@ -142,10 +142,25 @@ @{ log.request.uri }

@{ log.subdomain } + @{ log.performed_at }
- @{ log.response.status } - @{ log.response.reason } + + @{ log.response.status } - @{ log.response.reason } + + + @{ log.response.status } - @{ log.response.reason } + + + @{ log.response.status } - @{ log.response.reason } +
... @@ -162,7 +177,7 @@
-
+

@@ -185,7 +200,23 @@

- Status code: @{ currentLog.response?.status} + + Received at: @{ currentLog.performed_at } + + @{ currentLog.response?.status } - @{ currentLog.response?.reason } + + + @{ currentLog.response?.status } - @{ currentLog.response?.reason } + + + @{ currentLog.response?.status } - @{ currentLog.response?.reason } +

@@ -225,7 +256,7 @@
-
+
@{ name }
@@ -241,7 +272,7 @@
-
+
@{ parameter.name }
@@ -259,7 +290,7 @@
-
+
@{ header }
@@ -281,7 +312,7 @@
-
+
@{ header }
@@ -297,7 +328,7 @@
-
+
@{ key }
diff --git a/tests/Feature/Server/AdminTest.php b/tests/Feature/Server/AdminTest.php index 7d79805..b744361 100644 --- a/tests/Feature/Server/AdminTest.php +++ b/tests/Feature/Server/AdminTest.php @@ -70,7 +70,7 @@ class AdminTest extends TestCase $this->app['config']['expose.admin.validate_auth_tokens'] = false; /** @var ResponseInterface $response */ - $this->await($this->browser->post('http://127.0.0.1:8080/settings', [ + $this->await($this->browser->post('http://127.0.0.1:8080/api/settings', [ 'Host' => 'expose.localhost', 'Authorization' => base64_encode("username:secret"), 'Content-Type' => 'application/json' @@ -85,7 +85,7 @@ class AdminTest extends TestCase public function it_can_create_users() { /** @var Response $response */ - $response = $this->await($this->browser->post('http://127.0.0.1:8080/users', [ + $response = $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [ 'Host' => 'expose.localhost', 'Authorization' => base64_encode("username:secret"), 'Content-Type' => 'application/json' @@ -103,7 +103,7 @@ class AdminTest extends TestCase public function it_can_delete_users() { /** @var Response $response */ - $this->await($this->browser->post('http://127.0.0.1:8080/users', [ + $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [ 'Host' => 'expose.localhost', 'Authorization' => base64_encode("username:secret"), 'Content-Type' => 'application/json' @@ -112,7 +112,7 @@ class AdminTest extends TestCase ]))); - $this->await($this->browser->delete('http://127.0.0.1:8080/users/1', [ + $this->await($this->browser->delete('http://127.0.0.1:8080/api/users/1', [ 'Host' => 'expose.localhost', 'Authorization' => base64_encode("username:secret"), 'Content-Type' => 'application/json' @@ -125,7 +125,7 @@ class AdminTest extends TestCase public function it_can_list_all_users() { /** @var Response $response */ - $this->await($this->browser->post('http://127.0.0.1:8080/users', [ + $this->await($this->browser->post('http://127.0.0.1:8080/api/users', [ 'Host' => 'expose.localhost', 'Authorization' => base64_encode("username:secret"), 'Content-Type' => 'application/json'