NetworkedApplications: select the durable SQLite profile via storage_path
This commit is contained in:
1 parent
686f73ec9d
commit
e6aec57ad8
3 files changed
+79
-2
No files matched your search
@@ -22,6 +22,8 @@ namespace NetworkedKv
|
||||
std::string admin_address{};
|
||||
/// Bearer token required on the data plane; empty fails closed.
|
||||
std::string bearer_token{};
|
||||
/// Durable SQLite KV path. Empty selects the in-memory profile.
|
||||
std::string storage_path{};
|
||||
std::uint32_t max_connections{64};
|
||||
std::size_t max_request_bytes{1024ULL * 1024ULL};
|
||||
std::size_t max_scan_page{1000};
|
||||
@@ -108,7 +110,7 @@ namespace NetworkedKv
|
||||
std::unique_ptr<Void::Runtime::Host> m_owned_host{};
|
||||
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<KvStore> m_store{};
|
||||
std::unique_ptr<KvApi> m_api{};
|
||||
std::unique_ptr<Void::Net::HttpServer> m_server{};
|
||||
std::unique_ptr<Void::Net::HttpServer> m_admin_server{};
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "networked_kv/application.hpp"
|
||||
|
||||
#include "networked_kv/sqlite_store.hpp"
|
||||
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <charconv>
|
||||
@@ -194,7 +196,15 @@ namespace NetworkedKv
|
||||
NetworkedApplication app;
|
||||
app.m_config = std::move(config);
|
||||
app.m_resources = resources;
|
||||
app.m_store = std::make_unique<MemoryKvStore>();
|
||||
if (app.m_config.storage_path.empty()) {
|
||||
app.m_store = std::make_unique<MemoryKvStore>();
|
||||
} else {
|
||||
auto durable = SqliteKvStore::open(app.m_config.storage_path);
|
||||
if (!durable) {
|
||||
return std::unexpected{std::move(durable).error()};
|
||||
}
|
||||
app.m_store = std::move(*durable);
|
||||
}
|
||||
app.m_api = std::make_unique<KvApi>(
|
||||
*app.m_store,
|
||||
ApiConfig{.bearer_token = app.m_config.bearer_token,
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -160,6 +162,68 @@ namespace
|
||||
assert(app->stop().has_value());
|
||||
assert(app->admin_bound_port() == 0);
|
||||
}
|
||||
|
||||
void durable_profile_survives_restart()
|
||||
{
|
||||
const auto directory = std::filesystem::temp_directory_path() /
|
||||
("void-netapp-" + std::to_string(::getpid()));
|
||||
std::filesystem::remove_all(directory);
|
||||
std::filesystem::create_directories(directory);
|
||||
const std::string path = (directory / "kv.db").string();
|
||||
|
||||
{
|
||||
ApplicationResources resources;
|
||||
resources.ownership = ResourceOwnership::Managed;
|
||||
NetworkedConfig config;
|
||||
config.listen_address = "127.0.0.1:0";
|
||||
config.bearer_token = "secret";
|
||||
config.storage_path = path;
|
||||
auto app = NetworkedApplication::create(resources, config);
|
||||
assert(app.has_value());
|
||||
assert(app->start().has_value());
|
||||
|
||||
Void::Net::HttpClient client;
|
||||
const std::string base =
|
||||
"http://127.0.0.1:" + std::to_string(app->health().bound_port);
|
||||
Void::Net::Request put;
|
||||
put.method = Void::Net::Method::Put;
|
||||
put.url = base + "/v1/kv/persist";
|
||||
put.body = std::string{"durable"};
|
||||
put.headers.push_back(
|
||||
Void::Net::Header{"authorization", "Bearer secret"});
|
||||
put.headers.push_back(Void::Net::Header{"x-request-id", "p1"});
|
||||
put.headers.push_back(Void::Net::Header{"if-none-match", "*"});
|
||||
auto created = client.send(put);
|
||||
assert(created.has_value());
|
||||
assert(created->status == 200);
|
||||
assert(app->stop().has_value());
|
||||
}
|
||||
|
||||
{
|
||||
ApplicationResources resources;
|
||||
resources.ownership = ResourceOwnership::Managed;
|
||||
NetworkedConfig config;
|
||||
config.listen_address = "127.0.0.1:0";
|
||||
config.bearer_token = "secret";
|
||||
config.storage_path = path;
|
||||
auto app = NetworkedApplication::create(resources, config);
|
||||
assert(app.has_value());
|
||||
assert(app->start().has_value());
|
||||
|
||||
Void::Net::HttpClient client;
|
||||
const std::string base =
|
||||
"http://127.0.0.1:" + std::to_string(app->health().bound_port);
|
||||
auto got = client.get(
|
||||
base + "/v1/kv/persist",
|
||||
{Void::Net::Header{"authorization", "Bearer secret"}});
|
||||
assert(got.has_value());
|
||||
assert(got->status == 200);
|
||||
assert(got->body == "durable");
|
||||
assert(app->stop().has_value());
|
||||
}
|
||||
|
||||
std::filesystem::remove_all(directory);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
@@ -170,6 +234,7 @@ int main()
|
||||
embedded_requires_a_host();
|
||||
http_listener_serves_kv();
|
||||
admin_listener_is_separate();
|
||||
durable_profile_survives_restart();
|
||||
std::puts("networked-kv lifecycle ok");
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user