Skip to content

Handlers

Handlers are the functions that process HTTP requests and return responses. They contain your application logic.

Handler Signature

Every handler follows the same signature:

cpp
using http_handler_t = std::function<http_response_t(const http_request&)>;

The handler receives a read-only http_request and must build and return an http_response_t. The framework sends the response after the handler returns.


Inline Handler

Define the handler directly at the route registration point. Best for simple endpoints.

cpp
using namespace framework;
using namespace framework::clients::http;

app.register_endpoint(
    http_verb_t::get,
    "/hello",
    [](const http_request& req) -> http_response_t {
        auto res = helpers::make_base_http_response(req, http_status_t::ok);
        auto body = helpers::make_base_http_payload(200, "Hello!");
        helpers::finalize_response(res, body);
        return res;
    }
);

Named Function

Define the handler as a separate function. Best for complex logic, reuse across routes, or testability.

cpp
using namespace framework;
using namespace framework::clients::http;

http_response_t list_users(const http_request& req) {
    auto res = helpers::make_base_http_response(req, http_status_t::ok);
    auto body = helpers::make_base_http_payload(200, "ok");
    body["data"] = boost::json::parse(R"([{"id":1,"name":"Alice"}])");
    helpers::finalize_response(res, body);
    return res;
}

app.register_endpoint(http_verb_t::get, "/users", list_users);

Building Responses Manually

You can also build responses from scratch without the helper functions. Use this when you need full control over the response format.

cpp
using namespace framework;
using namespace framework::clients::http;

[](const auto& req) -> http_response_t {
    http_response_t res;
    res.result(http_status_t::ok);
    res.set(http_field_t::content_type, "application/json");
    res.body() = R"({"status":200,"message":"ok"})";
    res.prepare_payload();
    return res;
};

Important: Always call res.prepare_payload() before returning. If you use helpers::finalize_response(), it already calls prepare_payload() internally — do not call it again.


The Response Object Methods

cpp
res.result(status_code);          // Set HTTP status (http_status_t::ok, ::not_found, etc.)
res.set(field, value);            // Set a header by enum or string name
res.body() = string_content;      // Set the response body
res.prepare_payload();            // Finalize — must be called before returning

Built-in Handlers

The framework includes pre-built HTTP handlers for introspection and management. They are in the framework::clients::http::handlers namespace.

HandlerURLPurpose
get_this_pingGET /this/pingHealth check — returns pong
get_this_upGET /this/upUptime check
get_this_nodesGET /this/nodesList peer nodes in the mesh
get_this_configurationGET /this/configurationShow current runtime configuration
patch_this_configurationPATCH /this/configurationUpdate runtime configuration
get_this_cacheGET /this/cacheList cache entries
get_this_cache_by_keyGET /this/cache/:keyGet a specific cache entry
get_this_cache_metricsGET /this/cache/metricsCache metrics
get_this_channelsGET /this/channelsList Pub/Sub channels
get_this_channel_by_nameGET /this/channels/:nameGet a specific channel
get_this_channel_subscriptionsGET /this/channels/:name/subscriptionsChannel subscribers
get_this_watchersGET /this/watchersList active watchers
get_this_watchers_nodesGET /this/watchers/nodesNode watchers
get_this_watchers_clientsGET /this/watchers/clientsClient watchers
get_this_watchers_channelsGET /this/watchers/channelsChannel watchers
get_this_watchers_cacheGET /this/watchers/cacheCache watchers
get_this_mailboxesGET /this/mailboxesList mailbox queues
get_this_topologyGET /this/topologyShow mesh topology
get_this_loggingGET /this/loggingShow current log configuration
put_this_loggingPUT /this/loggingUpdate log configuration
get_this_queuesGET /this/queuesList job queues
get_this_queue_by_nameGET /this/queues/:nameGet a specific queue
get_this_queue_metricsGET /this/queues/:name/metricsQueue metrics
post_this_queue_pausePOST /this/queues/:name/pausePause a queue
post_this_queue_resumePOST /this/queues/:name/resumeResume a queue
post_this_queue_cancelPOST /this/queues/:name/cancelCancel a job by ID
post_this_queue_dispatchPOST /this/queues/:name/dispatchDispatch a job via HTTP

These handlers follow a consistent JSON response format. Register them manually via register_endpoint() with the corresponding URLs listed above.