get/put/post/del
This commit is contained in:
parent
47197b9491
commit
2d69c24ac4
3 changed files with 70 additions and 29 deletions
|
@ -3,26 +3,51 @@
|
||||||
|
|
||||||
seastar::future<bool> Database::stop() { return seastar::make_ready_future<bool>(true); }
|
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");
|
sstring rest = req->param.get_decoded_param("path");
|
||||||
rep->write_body("json", json::stream_object(rest));
|
rep->write_body("json", json::stream_object(co_await db.get(rest)));
|
||||||
return make_ready_future<std::unique_ptr<http::reply>>(std::move(rep));
|
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");
|
sstring rest = req->param.get_decoded_param("path");
|
||||||
rep->write_body("json", json::stream_object(rest));
|
sstring value = req->query_parameters.find("value")->first;
|
||||||
return make_ready_future<std::unique_ptr<http::reply>>(std::move(rep));
|
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");
|
sstring rest = req->param.get_decoded_param("path");
|
||||||
rep->write_body("json", json::stream_object(rest));
|
rep->write_body("json", json::stream_object(co_await db.post(rest, req->content_stream)));
|
||||||
return make_ready_future<std::unique_ptr<http::reply>>(std::move(rep));
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,25 +5,24 @@
|
||||||
using namespace seastar;
|
using namespace seastar;
|
||||||
using namespace httpd;
|
using namespace httpd;
|
||||||
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
class Database {
|
class Database {
|
||||||
|
|
||||||
seastar::httpd::http_server_control& srv;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Database(seastar::httpd::http_server_control& srv)
|
explicit Database() = default;
|
||||||
: srv(srv) {}
|
|
||||||
|
|
||||||
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 {
|
class GetHandler : public handler_base {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
explicit GetHandler(Database& db);
|
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:
|
private:
|
||||||
Database& db;
|
Database& db;
|
||||||
|
@ -31,10 +30,21 @@ private:
|
||||||
|
|
||||||
class PutHandler : public handler_base {
|
class PutHandler : public handler_base {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
explicit PutHandler(Database& db);
|
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:
|
private:
|
||||||
Database& db;
|
Database& db;
|
||||||
|
@ -42,12 +52,11 @@ private:
|
||||||
|
|
||||||
class DeleteHandler : public handler_base {
|
class DeleteHandler : public handler_base {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
explicit DeleteHandler(Database& db);
|
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:
|
private:
|
||||||
Database& db;
|
Database& db;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
13
src/noidb.cc
13
src/noidb.cc
|
@ -1,12 +1,10 @@
|
||||||
|
|
||||||
#include "api_handler.hh"
|
|
||||||
#include "database.hh"
|
#include "database.hh"
|
||||||
|
|
||||||
#include <seastar/apps/lib/stop_signal.hh>
|
#include <seastar/apps/lib/stop_signal.hh>
|
||||||
#include <seastar/core/app-template.hh>
|
#include <seastar/core/app-template.hh>
|
||||||
#include <seastar/core/reactor.hh>
|
#include <seastar/core/reactor.hh>
|
||||||
#include <seastar/core/thread.hh>
|
#include <seastar/core/thread.hh>
|
||||||
#include <seastar/http/function_handlers.hh>
|
|
||||||
#include <seastar/http/httpd.hh>
|
#include <seastar/http/httpd.hh>
|
||||||
#include <seastar/http/request.hh>
|
#include <seastar/http/request.hh>
|
||||||
#include <seastar/http/routes.hh>
|
#include <seastar/http/routes.hh>
|
||||||
|
@ -42,7 +40,7 @@ int main(int argc, char** argv) {
|
||||||
seastar::httpd::http_server_control srv;
|
seastar::httpd::http_server_control srv;
|
||||||
srv.start().get();
|
srv.start().get();
|
||||||
|
|
||||||
Database db(srv);
|
Database db;
|
||||||
|
|
||||||
srv
|
srv
|
||||||
.set_routes([&db](seastar::httpd::routes& r) {
|
.set_routes([&db](seastar::httpd::routes& r) {
|
||||||
|
@ -62,6 +60,15 @@ int main(int argc, char** argv) {
|
||||||
})
|
})
|
||||||
.get();
|
.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
|
srv
|
||||||
.set_routes([&db](seastar::httpd::routes& r) {
|
.set_routes([&db](seastar::httpd::routes& r) {
|
||||||
r.add(
|
r.add(
|
||||||
|
|
Loading…
Reference in a new issue