Line data Source code
1 : #ifndef THINGER_HTTP_SERVER_DETAIL_SPAWN_HANDLER_HPP
2 : #define THINGER_HTTP_SERVER_DETAIL_SPAWN_HANDLER_HPP
3 :
4 : #include <exception>
5 : #include <utility>
6 : #include <boost/asio/co_spawn.hpp>
7 :
8 : namespace thinger::http::detail {
9 :
10 : // Log what a coroutine run by spawn_handler() threw, as thrown by the `what` handler
11 : void log_handler_exception(const char* what, const std::exception_ptr& error);
12 :
13 : // Run a coroutine (or a function returning one) on `executor` (or an io_context), detached:
14 : // what it throws is logged. Not public: the response and the file server use it, from their
15 : // .cpp files, for the handlers and bodies that go on once the request handler returned.
16 : template<typename Executor, typename F>
17 104 : void spawn_handler(Executor&& executor, const char* what, F&& run) {
18 104 : boost::asio::co_spawn(std::forward<Executor>(executor), std::forward<F>(run),
19 218 : [what](const std::exception_ptr& error) {
20 104 : if (error) log_handler_exception(what, error);
21 : });
22 104 : }
23 :
24 : } // namespace thinger::http::detail
25 :
26 : #endif // THINGER_HTTP_SERVER_DETAIL_SPAWN_HANDLER_HPP
|