get/put/delete

This commit is contained in:
Gregory Burd 2024-05-01 15:55:17 -04:00
parent e24f034fd9
commit d691b469bf
3 changed files with 47 additions and 12 deletions

View file

@ -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)

28
src/database.cc Normal file
View file

@ -0,0 +1,28 @@
#include "database.hh"
seastar::future<bool> Database::stop() { return seastar::make_ready_future<bool>(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));
}
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) {
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));
}
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(rest));
return make_ready_future<std::unique_ptr<http::reply>>(std::move(rep));
}

View file

@ -1,12 +1,11 @@
#include "Database.hh"
#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/sleep.hh>
#include <seastar/core/thread.hh>
#include <seastar/http/file_handler.hh>
#include <seastar/http/function_handlers.hh>
#include <seastar/http/httpd.hh>
#include <seastar/http/request.hh>
@ -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();