From d691b469bf4be779fa55b6abed51094d09d384d6 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Wed, 1 May 2024 15:55:17 -0400 Subject: [PATCH] get/put/delete --- src/CMakeLists.txt | 2 +- src/database.cc | 28 ++++++++++++++++++++++++++++ src/noidb.cc | 29 ++++++++++++++++++----------- 3 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 src/database.cc diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0042c69..57ffead 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,3 @@ add_executable(noidb) -target_sources(noidb PRIVATE noidb.cc Database.cc) +target_sources(noidb PRIVATE noidb.cc database.cc api_handler.cc) target_link_libraries(noidb PRIVATE Seastar::seastar) diff --git a/src/database.cc b/src/database.cc new file mode 100644 index 0000000..7e67cf3 --- /dev/null +++ b/src/database.cc @@ -0,0 +1,28 @@ + +#include "database.hh" + +seastar::future Database::stop() { return seastar::make_ready_future(true); } + +GetHandler::GetHandler(Database& db) : db(db) {} + +future> GetHandler::handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) { + sstring rest = req->param.get_decoded_param("path"); + rep->write_body("json", json::stream_object(rest)); + return make_ready_future>(std::move(rep)); +} + +PutHandler::PutHandler(Database& db) : db(db) {} + +future> PutHandler::handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) { + sstring rest = req->param.get_decoded_param("path"); + rep->write_body("json", json::stream_object(rest)); + return make_ready_future>(std::move(rep)); +} + +DeleteHandler::DeleteHandler(Database& db) : db(db) {} + +future> DeleteHandler::handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) { + sstring rest = req->param.get_decoded_param("path"); + rep->write_body("json", json::stream_object(rest)); + return make_ready_future>(std::move(rep)); +} diff --git a/src/noidb.cc b/src/noidb.cc index 5d0d631..7864323 100644 --- a/src/noidb.cc +++ b/src/noidb.cc @@ -1,12 +1,11 @@ -#include "Database.hh" +#include "api_handler.hh" +#include "database.hh" #include #include #include -#include #include -#include #include #include #include @@ -46,21 +45,29 @@ int main(int argc, char** argv) { Database db(srv); srv - .set_routes([](seastar::httpd::routes& r) { + .set_routes([&db](seastar::httpd::routes& r) { r.add( seastar::httpd::operation_type::GET, - seastar::httpd::url("/hello"), - new seastar::httpd::function_handler( - []([[maybe_unused]] seastar::httpd::const_req req) { return "hi"; })); + seastar::httpd::url("/kv").remainder("path"), + new GetHandler(db)); }) .get(); srv - .set_routes([](seastar::httpd::routes& r) { + .set_routes([&db](seastar::httpd::routes& r) { r.add( - seastar::httpd::operation_type::GET, - seastar::httpd::url("").remainder("path"), - new seastar::httpd::directory_handler("./public/")); + seastar::httpd::operation_type::PUT, + seastar::httpd::url("/kv").remainder("path"), + new PutHandler(db)); + }) + .get(); + + srv + .set_routes([&db](seastar::httpd::routes& r) { + r.add( + seastar::httpd::operation_type::DELETE, + seastar::httpd::url("/kv").remainder("path"), + new DeleteHandler(db)); }) .get();