NetworkedApplications: serve KvApi behind a real Net HTTP listener
This commit is contained in:
1 parent
fd763e4feb
commit
183b8033fd
4 files changed
+204
-1
No files matched your search
@@ -14,7 +14,7 @@ add_library(networked_kv STATIC
|
|||||||
src/http_api.cpp
|
src/http_api.cpp
|
||||||
src/memory_store.cpp)
|
src/memory_store.cpp)
|
||||||
target_include_directories(networked_kv PUBLIC include)
|
target_include_directories(networked_kv PUBLIC include)
|
||||||
target_link_libraries(networked_kv PUBLIC Void::Runtime)
|
target_link_libraries(networked_kv PUBLIC Void::Net Void::Runtime)
|
||||||
|
|
||||||
add_executable(networked_kv_test tests/application_test.cpp)
|
add_executable(networked_kv_test tests/application_test.cpp)
|
||||||
target_link_libraries(networked_kv_test PRIVATE networked_kv)
|
target_link_libraries(networked_kv_test PRIVATE networked_kv)
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "networked_kv/domain.hpp"
|
||||||
|
#include "networked_kv/http_api.hpp"
|
||||||
|
|
||||||
#include <Void/Functional/Result.hpp>
|
#include <Void/Functional/Result.hpp>
|
||||||
|
#include <Void/Net/HttpServer.hpp>
|
||||||
#include <Void/Runtime/Host.hpp>
|
#include <Void/Runtime/Host.hpp>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@@ -16,6 +20,8 @@ namespace NetworkedKv
|
|||||||
struct NetworkedConfig {
|
struct NetworkedConfig {
|
||||||
std::string listen_address{"127.0.0.1:0"};
|
std::string listen_address{"127.0.0.1:0"};
|
||||||
std::string admin_address{};
|
std::string admin_address{};
|
||||||
|
/// Bearer token required on the data plane; empty fails closed.
|
||||||
|
std::string bearer_token{};
|
||||||
std::uint32_t max_connections{64};
|
std::uint32_t max_connections{64};
|
||||||
std::size_t max_request_bytes{1024ULL * 1024ULL};
|
std::size_t max_request_bytes{1024ULL * 1024ULL};
|
||||||
std::size_t max_scan_page{1000};
|
std::size_t max_scan_page{1000};
|
||||||
@@ -52,6 +58,7 @@ namespace NetworkedKv
|
|||||||
bool ready{false};
|
bool ready{false};
|
||||||
bool managed{false};
|
bool managed{false};
|
||||||
std::size_t started_workers{0};
|
std::size_t started_workers{0};
|
||||||
|
std::uint16_t bound_port{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Composition surface for the networked key-value reference service.
|
/// Composition surface for the networked key-value reference service.
|
||||||
@@ -86,6 +93,8 @@ namespace NetworkedKv
|
|||||||
{
|
{
|
||||||
return m_config;
|
return m_config;
|
||||||
}
|
}
|
||||||
|
/// Port the HTTP listener actually bound (0 when not listening).
|
||||||
|
[[nodiscard]] std::uint16_t bound_port() const noexcept;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@@ -96,6 +105,10 @@ namespace NetworkedKv
|
|||||||
ApplicationResources m_resources{};
|
ApplicationResources m_resources{};
|
||||||
std::unique_ptr<Void::Runtime::Host> m_owned_host{};
|
std::unique_ptr<Void::Runtime::Host> m_owned_host{};
|
||||||
Void::Runtime::Host* m_host{nullptr};
|
Void::Runtime::Host* m_host{nullptr};
|
||||||
|
// Heap-owned so a moved application keeps the KvApi reference valid.
|
||||||
|
std::unique_ptr<MemoryKvStore> m_store{};
|
||||||
|
std::unique_ptr<KvApi> m_api{};
|
||||||
|
std::unique_ptr<Void::Net::HttpServer> m_server{};
|
||||||
ApplicationState m_state{ApplicationState::Created};
|
ApplicationState m_state{ApplicationState::Created};
|
||||||
};
|
};
|
||||||
} // namespace NetworkedKv
|
} // namespace NetworkedKv
|
||||||
@@ -2,7 +2,10 @@
|
|||||||
|
|
||||||
#include <Void/Time/Clock.hpp>
|
#include <Void/Time/Clock.hpp>
|
||||||
|
|
||||||
|
#include <charconv>
|
||||||
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace NetworkedKv
|
namespace NetworkedKv
|
||||||
@@ -22,6 +25,71 @@ namespace NetworkedKv
|
|||||||
std::move(message)};
|
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
|
[[nodiscard]] Void::Async::OperationOptions
|
||||||
deadline_after(std::chrono::milliseconds grace)
|
deadline_after(std::chrono::milliseconds grace)
|
||||||
{
|
{
|
||||||
@@ -66,6 +134,9 @@ namespace NetworkedKv
|
|||||||
, m_resources(other.m_resources)
|
, m_resources(other.m_resources)
|
||||||
, m_owned_host(std::move(other.m_owned_host))
|
, m_owned_host(std::move(other.m_owned_host))
|
||||||
, m_host(other.m_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)
|
, m_state(other.m_state)
|
||||||
{
|
{
|
||||||
other.m_host = nullptr;
|
other.m_host = nullptr;
|
||||||
@@ -81,6 +152,9 @@ namespace NetworkedKv
|
|||||||
m_resources = other.m_resources;
|
m_resources = other.m_resources;
|
||||||
m_owned_host = std::move(other.m_owned_host);
|
m_owned_host = std::move(other.m_owned_host);
|
||||||
m_host = other.m_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;
|
m_state = other.m_state;
|
||||||
other.m_host = nullptr;
|
other.m_host = nullptr;
|
||||||
other.m_state = ApplicationState::Stopped;
|
other.m_state = ApplicationState::Stopped;
|
||||||
@@ -90,6 +164,10 @@ namespace NetworkedKv
|
|||||||
|
|
||||||
NetworkedApplication::~NetworkedApplication()
|
NetworkedApplication::~NetworkedApplication()
|
||||||
{
|
{
|
||||||
|
if (m_server) {
|
||||||
|
m_server->shutdown(m_config.shutdown_grace);
|
||||||
|
m_server.reset();
|
||||||
|
}
|
||||||
stop_owned_host();
|
stop_owned_host();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,6 +188,12 @@ namespace NetworkedKv
|
|||||||
NetworkedApplication app;
|
NetworkedApplication app;
|
||||||
app.m_config = std::move(config);
|
app.m_config = std::move(config);
|
||||||
app.m_resources = resources;
|
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;
|
return app;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,6 +229,54 @@ namespace NetworkedKv
|
|||||||
"borrowed host")};
|
"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;
|
m_state = ApplicationState::Running;
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@@ -160,6 +292,10 @@ namespace NetworkedKv
|
|||||||
return Void::Runtime::ShutdownReport{};
|
return Void::Runtime::ShutdownReport{};
|
||||||
}
|
}
|
||||||
m_state = ApplicationState::Stopping;
|
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) {
|
if (m_resources.ownership == ResourceOwnership::Managed && m_host) {
|
||||||
auto report =
|
auto report =
|
||||||
m_host->shutdown_blocking(deadline_after(m_config.shutdown_grace));
|
m_host->shutdown_blocking(deadline_after(m_config.shutdown_grace));
|
||||||
@@ -185,9 +321,15 @@ namespace NetworkedKv
|
|||||||
snapshot.ready = m_host->is_ready();
|
snapshot.ready = m_host->is_ready();
|
||||||
snapshot.started_workers = m_host->started_worker_count();
|
snapshot.started_workers = m_host->started_worker_count();
|
||||||
}
|
}
|
||||||
|
snapshot.bound_port = bound_port();
|
||||||
return snapshot;
|
return snapshot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::uint16_t NetworkedApplication::bound_port() const noexcept
|
||||||
|
{
|
||||||
|
return m_server ? m_server->bound_port() : 0;
|
||||||
|
}
|
||||||
|
|
||||||
void NetworkedApplication::stop_owned_host() noexcept
|
void NetworkedApplication::stop_owned_host() noexcept
|
||||||
{
|
{
|
||||||
if (m_resources.ownership != ResourceOwnership::Managed ||
|
if (m_resources.ownership != ResourceOwnership::Managed ||
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
#include "networked_kv/application.hpp"
|
#include "networked_kv/application.hpp"
|
||||||
|
|
||||||
|
#include <Void/Net/Http.hpp>
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
@@ -73,6 +76,50 @@ namespace
|
|||||||
auto app = NetworkedApplication::create(resources, NetworkedConfig{});
|
auto app = NetworkedApplication::create(resources, NetworkedConfig{});
|
||||||
assert(!app.has_value());
|
assert(!app.has_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void http_listener_serves_kv()
|
||||||
|
{
|
||||||
|
ApplicationResources resources;
|
||||||
|
resources.ownership = ResourceOwnership::Managed;
|
||||||
|
NetworkedConfig config;
|
||||||
|
config.listen_address = "127.0.0.1:0";
|
||||||
|
config.bearer_token = "secret";
|
||||||
|
auto app = NetworkedApplication::create(resources, config);
|
||||||
|
assert(app.has_value());
|
||||||
|
assert(app->start().has_value());
|
||||||
|
|
||||||
|
const auto port = app->health().bound_port;
|
||||||
|
assert(port != 0);
|
||||||
|
const std::string base =
|
||||||
|
"http://127.0.0.1:" + std::to_string(port);
|
||||||
|
Void::Net::HttpClient client;
|
||||||
|
|
||||||
|
auto unauthorized = client.get(base + "/v1/kv/a");
|
||||||
|
assert(unauthorized.has_value());
|
||||||
|
assert(unauthorized->status == 401);
|
||||||
|
|
||||||
|
Void::Net::Request put;
|
||||||
|
put.method = Void::Net::Method::Put;
|
||||||
|
put.url = base + "/v1/kv/a";
|
||||||
|
put.body = std::string{"one"};
|
||||||
|
put.headers.push_back(
|
||||||
|
Void::Net::Header{"authorization", "Bearer secret"});
|
||||||
|
put.headers.push_back(Void::Net::Header{"x-request-id", "r1"});
|
||||||
|
put.headers.push_back(Void::Net::Header{"if-none-match", "*"});
|
||||||
|
auto created = client.send(put);
|
||||||
|
assert(created.has_value());
|
||||||
|
assert(created->status == 200);
|
||||||
|
|
||||||
|
auto got = client.get(
|
||||||
|
base + "/v1/kv/a",
|
||||||
|
{Void::Net::Header{"authorization", "Bearer secret"}});
|
||||||
|
assert(got.has_value());
|
||||||
|
assert(got->status == 200);
|
||||||
|
assert(got->body == "one");
|
||||||
|
|
||||||
|
assert(app->stop().has_value());
|
||||||
|
assert(app->health().bound_port == 0);
|
||||||
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@@ -81,6 +128,7 @@ int main()
|
|||||||
managed_mode_owns_the_host();
|
managed_mode_owns_the_host();
|
||||||
embedded_mode_borrows_the_host();
|
embedded_mode_borrows_the_host();
|
||||||
embedded_requires_a_host();
|
embedded_requires_a_host();
|
||||||
|
http_listener_serves_kv();
|
||||||
std::puts("networked-kv lifecycle ok");
|
std::puts("networked-kv lifecycle ok");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user