Skip to content

Commit

Permalink
Add health check endpoint (#190) (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirills-morozovs authored May 9, 2024
1 parent d5783a3 commit f0cee42
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Protocols/Pusher/Http/Controllers/HealthCheckController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravel\Reverb\Protocols\Pusher\Http\Controllers;

use Laravel\Reverb\Servers\Reverb\Http\Connection;
use Laravel\Reverb\Servers\Reverb\Http\Response;
use Psr\Http\Message\RequestInterface;

class HealthCheckController extends Controller
{
/**
* Handle the request.
*/
public function __invoke(RequestInterface $request, Connection $connection): Response
{
return new Response((object) ['health' => 'OK']);
}
}
2 changes: 2 additions & 0 deletions src/Servers/Reverb/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Laravel\Reverb\Protocols\Pusher\Http\Controllers\ConnectionsController;
use Laravel\Reverb\Protocols\Pusher\Http\Controllers\EventsBatchController;
use Laravel\Reverb\Protocols\Pusher\Http\Controllers\EventsController;
use Laravel\Reverb\Protocols\Pusher\Http\Controllers\HealthCheckController;
use Laravel\Reverb\Protocols\Pusher\Http\Controllers\PusherController;
use Laravel\Reverb\Protocols\Pusher\Http\Controllers\UsersTerminateController;
use Laravel\Reverb\Protocols\Pusher\Managers\ArrayChannelConnectionManager;
Expand Down Expand Up @@ -101,6 +102,7 @@ protected static function pusherRoutes(): RouteCollection
$routes->add('channel', Route::get('/apps/{appId}/channels/{channel}', new ChannelController));
$routes->add('channel_users', Route::get('/apps/{appId}/channels/{channel}/users', new ChannelUsersController));
$routes->add('users_terminate', Route::post('/apps/{appId}/users/{userId}/terminate_connections', new UsersTerminateController));
$routes->add('health_check', Route::get('/up', new HealthCheckController));

return $routes;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Laravel\Reverb\Tests\ReverbTestCase;

use function React\Async\await;

uses(ReverbTestCase::class);

it('fails when server is not running', function () {
$this->stopServer();
await($this->requestWithoutAppId('up'));
})->throws(RuntimeException::class);

it('can respond to a health check request', function () {
$response = await($this->requestWithoutAppId('up'));

expect($response->getStatusCode())->toBe(200);
expect($response->getBody()->getContents())->toBe('{"health":"OK"}');
expect($response->getHeader('Content-Length'))->toBe(['15']);
});
14 changes: 14 additions & 0 deletions tests/ReverbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,20 @@ public function request(string $path, string $method = 'GET', mixed $data = '',
);
}

/**
* Send a request to the server without specifying app ID.
*/
public function requestWithoutAppId(string $path, string $method = 'GET', mixed $data = '', string $host = '0.0.0.0', string $port = '8080'): PromiseInterface
{
return (new Browser($this->loop))
->request(
$method,
"http://{$host}:{$port}/{$path}",
[],
($data) ? json_encode($data) : ''
);
}

/**
* Send a signed request to the server.
*/
Expand Down

0 comments on commit f0cee42

Please sign in to comment.