NetworkedApplications: serve KvApi behind a real Net HTTP listener

This commit is contained in:
faumaray committed 2026-12-03 20:29:09 +03:00
1 parent fd763e4feb
commit 183b8033fd
4 files changed
+204 -1

No files matched your search

@@ -2,7 +2,10 @@
#include <Void/Time/Clock.hpp>
#include <charconv>
#include <cstdint>
#include <string>
#include <string_view>
#include <utility>
namespace NetworkedKv
@@ -22,6 +25,71 @@ namespace NetworkedKv
std::move(message)};
}
[[nodiscard]] bool parse_address(std::string_view address, std::string& host,
std::uint16_t& port) noexcept
{
const auto colon = address.rfind(':');
if (colon == std::string_view::npos || colon == 0 ||
colon + 1 >= address.size()) {
return false;
}
host = std::string{address.substr(0, colon)};
const auto digits = address.substr(colon + 1);
unsigned int value = 0;
const auto parsed = std::from_chars(
digits.data(), digits.data() + digits.size(), value);
if (parsed.ec != std::errc{} ||
parsed.ptr != digits.data() + digits.size() || value > 65535U) {
return false;
}
port = static_cast<std::uint16_t>(value);
return true;
}
[[nodiscard]] std::string method_name(Void::Net::Method method)
{
switch (method) {
case Void::Net::Method::Get: return "GET";
case Void::Net::Method::Post: return "POST";
case Void::Net::Method::Put: return "PUT";
case Void::Net::Method::Delete: return "DELETE";
case Void::Net::Method::Patch: return "PATCH";
case Void::Net::Method::Head: return "HEAD";
case Void::Net::Method::Options: return "OPTIONS";
}
return "GET";
}
[[nodiscard]] HttpRequest
to_http_request(const Void::Net::ServerRequest& request)
{
HttpRequest out;
out.method = method_name(request.method);
out.target = request.path;
if (!request.query.empty()) {
out.target += '?';
out.target += request.query;
}
for (const auto& header : request.headers) {
out.headers[header.name] = header.value;
}
out.body = request.body;
return out;
}
[[nodiscard]] Void::Net::ServerResponse
to_server_response(const HttpResponse& response)
{
Void::Net::ServerResponse out;
out.status = static_cast<std::uint16_t>(response.status);
out.body = response.body;
out.headers.reserve(response.headers.size());
for (const auto& [name, value] : response.headers) {
out.headers.push_back(Void::Net::Header{name, value});
}
return out;
}
[[nodiscard]] Void::Async::OperationOptions
deadline_after(std::chrono::milliseconds grace)
{
@@ -66,6 +134,9 @@ namespace NetworkedKv
, m_resources(other.m_resources)
, m_owned_host(std::move(other.m_owned_host))
, m_host(other.m_host)
, m_store(std::move(other.m_store))
, m_api(std::move(other.m_api))
, m_server(std::move(other.m_server))
, m_state(other.m_state)
{
other.m_host = nullptr;
@@ -81,6 +152,9 @@ namespace NetworkedKv
m_resources = other.m_resources;
m_owned_host = std::move(other.m_owned_host);
m_host = other.m_host;
m_store = std::move(other.m_store);
m_api = std::move(other.m_api);
m_server = std::move(other.m_server);
m_state = other.m_state;
other.m_host = nullptr;
other.m_state = ApplicationState::Stopped;
@@ -90,6 +164,10 @@ namespace NetworkedKv
NetworkedApplication::~NetworkedApplication()
{
if (m_server) {
m_server->shutdown(m_config.shutdown_grace);
m_server.reset();
}
stop_owned_host();
}
@@ -110,6 +188,12 @@ namespace NetworkedKv
NetworkedApplication app;
app.m_config = std::move(config);
app.m_resources = resources;
app.m_store = std::make_unique<MemoryKvStore>();
app.m_api = std::make_unique<KvApi>(
*app.m_store,
ApiConfig{.bearer_token = app.m_config.bearer_token,
.max_body_bytes = app.m_config.max_request_bytes,
.max_scan_limit = app.m_config.max_scan_page});
return app;
}
@@ -145,6 +229,54 @@ namespace NetworkedKv
"borrowed host")};
}
}
if (!m_server) {
std::string host;
std::uint16_t port = 0;
if (!parse_address(m_config.listen_address, host, port)) {
m_state = ApplicationState::Failed;
return std::unexpected{config_error(
"NetworkedApplication: listen_address must be host:port")};
}
Void::Net::HttpServerOptions options;
options.bind_host = std::move(host);
options.port = port;
options.max_body_bytes = m_config.max_request_bytes;
options.max_concurrent_connections = m_config.max_connections;
auto server = std::make_unique<Void::Net::HttpServer>(options);
auto add = [&](Void::Net::Method method, const char* pattern) {
return server->add_route(
method, pattern,
[this](const Void::Net::ServerRequest& request)
-> Void::Net::RouteResult {
return to_server_response(
m_api->handle(to_http_request(request)));
});
};
Void::Result::Error route_error{};
bool routes_ok = true;
auto add_checked = [&](Void::Net::Method method, const char* pattern) {
if (!routes_ok) {
return;
}
if (auto added = add(method, pattern); !added) {
routes_ok = false;
route_error = std::move(added).error();
}
};
add_checked(Void::Net::Method::Get, "/v1/kv/:key");
add_checked(Void::Net::Method::Put, "/v1/kv/:key");
add_checked(Void::Net::Method::Delete, "/v1/kv/:key");
add_checked(Void::Net::Method::Get, "/v1/scan");
if (!routes_ok) {
m_state = ApplicationState::Failed;
return std::unexpected{std::move(route_error)};
}
if (auto started = server->start(); !started) {
m_state = ApplicationState::Failed;
return started;
}
m_server = std::move(server);
}
m_state = ApplicationState::Running;
return {};
}
@@ -160,6 +292,10 @@ namespace NetworkedKv
return Void::Runtime::ShutdownReport{};
}
m_state = ApplicationState::Stopping;
if (m_server) {
m_server->shutdown(m_config.shutdown_grace);
m_server.reset();
}
if (m_resources.ownership == ResourceOwnership::Managed && m_host) {
auto report =
m_host->shutdown_blocking(deadline_after(m_config.shutdown_grace));
@@ -185,9 +321,15 @@ namespace NetworkedKv
snapshot.ready = m_host->is_ready();
snapshot.started_workers = m_host->started_worker_count();
}
snapshot.bound_port = bound_port();
return snapshot;
}
std::uint16_t NetworkedApplication::bound_port() const noexcept
{
return m_server ? m_server->bound_port() : 0;
}
void NetworkedApplication::stop_owned_host() noexcept
{
if (m_resources.ownership != ResourceOwnership::Managed ||