|
| 1 | +#include "http_hnd_db.hpp" |
| 2 | +#include "http_hnd_db.pb.h" |
| 3 | +#include "db_server.hpp" |
| 4 | +#include "db_server_impl.hpp" |
| 5 | +#include "http_server_impl.hpp" |
| 6 | +#include "log.hpp" |
| 7 | +#include <boost/asio/use_awaitable.hpp> |
| 8 | + |
| 9 | +namespace acppsrv::http_hnd { |
| 10 | + |
| 11 | +template <class Executor, |
| 12 | + boost::asio::completion_token_for<void(proto::db::Response)> CT> |
| 13 | +auto db::call_query(Executor executor, proto::db::Request&& request, CT&& token) |
| 14 | +{ |
| 15 | + auto init = |
| 16 | + [this, executor, &request]<class Handler>(Handler&& handler) mutable { |
| 17 | + db_srv.query(std::move(request), |
| 18 | + [executor, handler = std::forward<Handler>(handler)]( |
| 19 | + proto::db::Response response |
| 20 | + ) mutable { |
| 21 | + boost::asio::post(executor, |
| 22 | + [handler = std::move(handler), |
| 23 | + response = std::move(response)]() mutable { |
| 24 | + handler(std::move(response)); |
| 25 | + }); |
| 26 | + }); |
| 27 | + }; |
| 28 | + return boost::asio::async_initiate<CT, void(proto::db::Response)>(init, |
| 29 | + token); |
| 30 | +} |
| 31 | + |
| 32 | +boost::asio::awaitable<http_handler::http_response_type> |
| 33 | +// NOLINTNEXTLINE(cppcoreguidelines-avoid-reference-coroutine-parameters) |
| 34 | +db::handle_async(const http_request_type& request, uint64_t sid, uint64_t req_n) |
| 35 | +{ |
| 36 | + auto [in, response] = parse<proto::db::Request>(request, sid, req_n); |
| 37 | + if (!in) |
| 38 | + co_return response; |
| 39 | + auto executor = co_await boost::asio::this_coro::executor; |
| 40 | + proto::db::Response out = co_await call_query(executor, std::move(*in), |
| 41 | + boost::asio::use_awaitable); |
| 42 | + serialize(request, sid, req_n, out, response); |
| 43 | + co_return response; |
| 44 | +} |
| 45 | + |
| 46 | +} // namespace acppsrv::http_hnd |
0 commit comments