Boost C++ Libraries

PrevUpHomeNext

Function template rest

boost::taar::handler::rest — Adapt an arbitrary callable into an HTTP request handler.

Synopsis

// In header: <boost/taar/handler/rest.hpp>


template<typename CallableType, typename... ArgProvidersType> 
  decltype(auto) 
  rest(CallableType && callable, ArgProvidersType... arg_providers);

Description

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:

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")));


PrevUpHomeNext