![]() |
boost::taar::handler::rest — Adapt an arbitrary callable into an HTTP request handler.
// In header: <boost/taar/handler/rest.hpp> template<typename CallableType, typename... ArgProvidersType> decltype(auto) rest(CallableType && callable, ArgProvidersType... arg_providers);
rest wraps callable into a handler suitable for boost::taar::session::basic_http::register_request_handler. Each arg_provider is responsible for producing one argument of callable from the incoming request:
path_arg pulls a captured path-template placeholder.
query_arg pulls a URL query parameter.
query_list_arg pulls every occurrence of a query parameter into a container.
header_arg pulls a header value.
cookie_arg pulls a cookie value.
string_body_arg, json_body_arg, and url_encoded_form_data_arg read and decode the request body.
Leading parameters of callable whose type is T&, T const&, or std::shared_ptr<T> for a context type T in scope are injected by the session instead of coming from a provider: the per-connection context of a basic_http<T>, or a keyed context added by with_session. The number of providers must therefore equal the number of remaining parameters. Asking for a context type that is not in scope fails to compile at the registration site.
The return value of callable is converted to an HTTP response with boost::taar::response_from; user-defined return types work as long as they have a tag_invoke overload for boost::taar::response_from_tag.
struct conn_stats { int requests = 0; }; taar::session::basic_http<conn_stats> http_session; http_session.register_request_handler( method == http::verb::get && target == "/users/{id}", taar::handler::rest( [](conn_stats& stats, int id) { ++stats.requests; return load_user(id); }, taar::handler::path_arg("id")));