Skip to content

Commit

Permalink
BRAYNS-584: Support HTTP healthcheck. (#1206)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien4193 committed Oct 20, 2023
1 parent 16df8ef commit 6368a9e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 152 deletions.
22 changes: 0 additions & 22 deletions apps/BraynsHealthcheck/CMakeLists.txt

This file was deleted.

120 changes: 0 additions & 120 deletions apps/BraynsHealthcheck/main.cpp

This file was deleted.

9 changes: 6 additions & 3 deletions apps/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# BRAYNS applications
add_subdirectory(BraynsHealthcheck)
add_subdirectory(BraynsService)
# Copyright (c) 2015-2023, EPFL/Blue Brain Project
# All rights reserved. Do not distribute without permission.
# Adrien Fleury <adrien.fleury@epfl.ch>
#
# This file is part of Brayns <https://github.com/BlueBrain/Brayns>

add_subdirectory(BraynsService)
64 changes: 57 additions & 7 deletions brayns/network/socket/ServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,31 @@
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Net/SecureServerSocket.h>
#include <Poco/Net/WebSocket.h>
#include "Poco/Net/NetException.h"

#include <brayns/utils/Log.h>

#include <brayns/network/websocket/WebSocket.h>

namespace
{
class RequestHandler : public Poco::Net::HTTPRequestHandler
class HealthcheckHandler : public Poco::Net::HTTPRequestHandler
{
public:
explicit RequestHandler(brayns::SocketManager &manager):
virtual void handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response) override
{
(void)request;
brayns::Log::debug("Healthcheck.");
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_OK);
auto &stream = response.send();
stream << "Ok";
}
};

class WebSocketHandler : public Poco::Net::HTTPRequestHandler
{
public:
explicit WebSocketHandler(brayns::SocketManager &manager):
_manager(manager)
{
}
Expand All @@ -49,27 +63,59 @@ class RequestHandler : public Poco::Net::HTTPRequestHandler
{
try
{
auto poco = Poco::Net::WebSocket(request, response);
auto poco = _upgrade(request, response);
auto socket = std::make_shared<brayns::WebSocket>(poco);
auto client = brayns::ClientRef(std::move(socket));
_manager.run(client);
}
catch (const Poco::Exception &e)
{
brayns::Log::error("Websocket server connection failed: {}.", e.displayText());
brayns::Log::error("Error in websocket handler: {}.", e.displayText());
}
catch (const std::exception &e)
{
brayns::Log::error("Unexpected error during websocket server connection: {}.", e.what());
brayns::Log::error("Unexpected error in websocket handler: {}.", e.what());
}
catch (...)
{
brayns::Log::error("Unknown error during websocket server connection.");
brayns::Log::error("Unknown error in websocket handler.");
}
}

private:
brayns::SocketManager &_manager;

Poco::Net::WebSocket _upgrade(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response)
{
try
{
return Poco::Net::WebSocket(request, response);
}
catch (const Poco::Net::WebSocketException &e)
{
_badRequest(e.displayText(), response);
throw;
}
catch (...)
{
_internalError("Unexpected websocket handshake failure", response);
throw;
}
}

void _badRequest(const std::string &message, Poco::Net::HTTPServerResponse &response)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST);
auto &stream = response.send();
stream << message;
}

void _internalError(const std::string &message, Poco::Net::HTTPServerResponse &response)
{
response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
auto &stream = response.send();
stream << message;
}
};

class RequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory
Expand All @@ -83,7 +129,11 @@ class RequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory
virtual Poco::Net::HTTPRequestHandler *createRequestHandler(const Poco::Net::HTTPServerRequest &request) override
{
brayns::Log::debug("HTTP request from '{}'.", request.getHost());
return new RequestHandler(_manager);
if (request.getURI() == "/healthz")
{
return new HealthcheckHandler();
}
return new WebSocketHandler(_manager);
}

private:
Expand Down

0 comments on commit 6368a9e

Please sign in to comment.