From 2d69c24ac475400d1c9b61029e1b224563377448 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Wed, 1 May 2024 16:41:23 -0400 Subject: [PATCH] get/put/post/del --- src/database.cc | 49 +++++++++++++++++++++++++++++++++++++------------ src/database.hh | 37 +++++++++++++++++++++++-------------- src/noidb.cc | 13 ++++++++++--- 3 files changed, 70 insertions(+), 29 deletions(-) diff --git a/src/database.cc b/src/database.cc index 7e67cf3..91dd3b5 100644 --- a/src/database.cc +++ b/src/database.cc @@ -3,26 +3,51 @@ seastar::future Database::stop() { return seastar::make_ready_future(true); } -GetHandler::GetHandler(Database& db) : db(db) {} +future Database::get(const sstring& key) { co_return key; } -future> GetHandler::handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) { +future Database::put(const sstring& key, sstring& val) { co_return true; } + +future Database::post(const sstring& key, input_stream* val) { co_return true; } + +future Database::del(const sstring& key) { co_return 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)); + rep->write_body("json", json::stream_object(co_await db.get(rest))); + co_return std::move(rep); } -PutHandler::PutHandler(Database& db) : db(db) {} +PutHandler::PutHandler(Database& db) + : db(db) {} -future> PutHandler::handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) { +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)); + sstring value = req->query_parameters.find("value")->first; + rep->write_body("json", json::stream_object(co_await db.put(rest, value))); + co_return std::move(rep); } -DeleteHandler::DeleteHandler(Database& db) : db(db) {} +PostHandler::PostHandler(Database& db) + : db(db) {} -future> DeleteHandler::handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) { +future> +PostHandler::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)); + rep->write_body("json", json::stream_object(co_await db.post(rest, req->content_stream))); + co_return 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(co_await db.del(rest))); + co_return std::move(rep); } diff --git a/src/database.hh b/src/database.hh index 1005feb..db620bc 100644 --- a/src/database.hh +++ b/src/database.hh @@ -5,25 +5,24 @@ using namespace seastar; using namespace httpd; - #include class Database { - - seastar::httpd::http_server_control& srv; - public: - explicit Database(seastar::httpd::http_server_control& srv) - : srv(srv) {} + explicit Database() = default; - seastar::future stop(); + future stop(); + future get(const sstring& key); + future put(const sstring& key, sstring& val); + future post(const sstring& key, input_stream* val); + future del(const sstring& key); }; class GetHandler : public handler_base { public: - explicit GetHandler(Database& db); - future> handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) override; + future> + handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) override; private: Database& db; @@ -31,10 +30,21 @@ private: class PutHandler : public handler_base { public: - explicit PutHandler(Database& db); - future> handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) override; + future> + handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) override; + +private: + Database& db; +}; + +class PostHandler : public handler_base { +public: + explicit PostHandler(Database& db); + + future> + handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) override; private: Database& db; @@ -42,12 +52,11 @@ private: class DeleteHandler : public handler_base { public: - explicit DeleteHandler(Database& db); - future> handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) override; + future> + handle(const sstring& path, std::unique_ptr req, std::unique_ptr rep) override; private: Database& db; }; - diff --git a/src/noidb.cc b/src/noidb.cc index 7864323..f61c190 100644 --- a/src/noidb.cc +++ b/src/noidb.cc @@ -1,12 +1,10 @@ -#include "api_handler.hh" #include "database.hh" #include #include #include #include -#include #include #include #include @@ -42,7 +40,7 @@ int main(int argc, char** argv) { seastar::httpd::http_server_control srv; srv.start().get(); - Database db(srv); + Database db; srv .set_routes([&db](seastar::httpd::routes& r) { @@ -62,6 +60,15 @@ int main(int argc, char** argv) { }) .get(); + srv + .set_routes([&db](seastar::httpd::routes& r) { + r.add( + seastar::httpd::operation_type::POST, + seastar::httpd::url("/kv").remainder("path"), + new PostHandler(db)); + }) + .get(); + srv .set_routes([&db](seastar::httpd::routes& r) { r.add(