diff --git a/examples/platforms/networked-kv/include/networked_kv/application.hpp b/examples/platforms/networked-kv/include/networked_kv/application.hpp index bd01f6b..19eabc2 100644 --- a/examples/platforms/networked-kv/include/networked_kv/application.hpp +++ b/examples/platforms/networked-kv/include/networked_kv/application.hpp @@ -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 m_owned_host{}; Void::Runtime::Host* m_host{nullptr}; // Heap-owned so a moved application keeps the KvApi reference valid. - std::unique_ptr m_store{}; + std::unique_ptr m_store{}; std::unique_ptr m_api{}; std::unique_ptr m_server{}; std::unique_ptr m_admin_server{}; diff --git a/examples/platforms/networked-kv/src/application.cpp b/examples/platforms/networked-kv/src/application.cpp index d1c8f9c..e3c4be5 100644 --- a/examples/platforms/networked-kv/src/application.cpp +++ b/examples/platforms/networked-kv/src/application.cpp @@ -1,5 +1,7 @@ #include "networked_kv/application.hpp" +#include "networked_kv/sqlite_store.hpp" + #include #include @@ -194,7 +196,15 @@ namespace NetworkedKv NetworkedApplication app; app.m_config = std::move(config); app.m_resources = resources; - app.m_store = std::make_unique(); + if (app.m_config.storage_path.empty()) { + app.m_store = std::make_unique(); + } 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( *app.m_store, ApiConfig{.bearer_token = app.m_config.bearer_token, diff --git a/examples/platforms/networked-kv/tests/application_test.cpp b/examples/platforms/networked-kv/tests/application_test.cpp index c6dc1ca..d03b765 100644 --- a/examples/platforms/networked-kv/tests/application_test.cpp +++ b/examples/platforms/networked-kv/tests/application_test.cpp @@ -4,7 +4,9 @@ #include #include +#include #include +#include 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; }