get/put/post/del

This commit is contained in:
Gregory Burd 2024-05-01 16:41:23 -04:00
parent 47197b9491
commit 2d69c24ac4
3 changed files with 70 additions and 29 deletions

View file

@ -3,26 +3,51 @@
seastar::future<bool> Database::stop() { return seastar::make_ready_future<bool>(true); }
GetHandler::GetHandler(Database& db) : db(db) {}
future<sstring> Database::get(const sstring& key) { co_return key; }
future<std::unique_ptr<http::reply>> GetHandler::handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) {
future<bool> Database::put(const sstring& key, sstring& val) { co_return true; }
future<bool> Database::post(const sstring& key, input_stream<char>* val) { co_return true; }
future<bool> Database::del(const sstring& key) { co_return true; }
GetHandler::GetHandler(Database& db)
: db(db) {}
future<std::unique_ptr<http::reply>>
GetHandler::handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) {
sstring rest = req->param.get_decoded_param("path");
rep->write_body("json", json::stream_object(rest));
return make_ready_future<std::unique_ptr<http::reply>>(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<std::unique_ptr<http::reply>> PutHandler::handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) {
future<std::unique_ptr<http::reply>>
PutHandler::handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) {
sstring rest = req->param.get_decoded_param("path");
rep->write_body("json", json::stream_object(rest));
return make_ready_future<std::unique_ptr<http::reply>>(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<std::unique_ptr<http::reply>> DeleteHandler::handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) {
future<std::unique_ptr<http::reply>>
PostHandler::handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) {
sstring rest = req->param.get_decoded_param("path");
rep->write_body("json", json::stream_object(rest));
return make_ready_future<std::unique_ptr<http::reply>>(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<std::unique_ptr<http::reply>>
DeleteHandler::handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> 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);
}

View file

@ -5,25 +5,24 @@
using namespace seastar;
using namespace httpd;
#include <string>
class Database {
seastar::httpd::http_server_control& srv;
public:
explicit Database(seastar::httpd::http_server_control& srv)
: srv(srv) {}
explicit Database() = default;
seastar::future<bool> stop();
future<bool> stop();
future<sstring> get(const sstring& key);
future<bool> put(const sstring& key, sstring& val);
future<bool> post(const sstring& key, input_stream<char>* val);
future<bool> del(const sstring& key);
};
class GetHandler : public handler_base {
public:
explicit GetHandler(Database& db);
future<std::unique_ptr<http::reply>> handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) override;
future<std::unique_ptr<http::reply>>
handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) override;
private:
Database& db;
@ -31,10 +30,21 @@ private:
class PutHandler : public handler_base {
public:
explicit PutHandler(Database& db);
future<std::unique_ptr<http::reply>> handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) override;
future<std::unique_ptr<http::reply>>
handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) override;
private:
Database& db;
};
class PostHandler : public handler_base {
public:
explicit PostHandler(Database& db);
future<std::unique_ptr<http::reply>>
handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) override;
private:
Database& db;
@ -42,12 +52,11 @@ private:
class DeleteHandler : public handler_base {
public:
explicit DeleteHandler(Database& db);
future<std::unique_ptr<http::reply>> handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) override;
future<std::unique_ptr<http::reply>>
handle(const sstring& path, std::unique_ptr<http::request> req, std::unique_ptr<http::reply> rep) override;
private:
Database& db;
};

View file

@ -1,12 +1,10 @@
#include "api_handler.hh"
#include "database.hh"
#include <seastar/apps/lib/stop_signal.hh>
#include <seastar/core/app-template.hh>
#include <seastar/core/reactor.hh>
#include <seastar/core/thread.hh>
#include <seastar/http/function_handlers.hh>
#include <seastar/http/httpd.hh>
#include <seastar/http/request.hh>
#include <seastar/http/routes.hh>
@ -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(