Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add health check endpoint (laravel#190) #191

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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