ores.cpp.nats-sub-registrar.nats_registrar_implementation

Table of Contents

Per-entity NATS sub-registrar implementation. Defines register_<entity>_handlers() which creates the handler and wires its NATS subjects. nats-sub-registrar profile.

Each entity's registrar includes only its own handler header, so a compile failure in one entity's handler cannot block the rest of the service.

Model flags consumed by this template:

Custom-includes paste point: the marker <<paste:068323E2-D722-46F1-8B86-46BD32426003>> is emitted after the standard protocol include so an entity can pull in extra headers (e.g. a split <entity>_history_protocol.hpp). Provide them as #+begin_src cpp :name custom_includes :implements 068323E2-D722-46F1-8B86-46BD32426003.

Custom-subscription paste point: the marker <<paste:FB267332-79AB-4873-AFDA-CEEE68A0F398>> is emitted just before the return so an entity can wire hand-crafted subjects (beyond the standard list/save/remove/history) that are not modelled by codegen. Provide them in the entity model as #+begin_src cpp :name custom_subscriptions :implements FB267332-79AB-4873-AFDA-CEEE68A0F398; entities without such a block collapse the marker to nothing. Example: party wires its read_parties_for_cache subject this way.

See the Template variable reference for the complete list of available variables and their semantics.

1. Template

The full template source. Edit here and re-tangle with compass build --direct tangle_codegen_templates to regenerate library/templates/cpp_nats_registrar.cpp.mustache.

{{! GENERATED FILE — tangled from projects/ores.codegen/library/templates/ores.cpp.nats-sub-registrar.nats_registrar_implementation.org. Edit the org source. }}
{{{cpp_license}}}
{{#domain_entity}}
#include "ores.{{component_core}}/messaging/{{entity_singular}}_registrar.hpp"
#include "ores.{{component_core}}/messaging/{{entity_singular}}_handler.hpp"
#include "ores.{{component_include}}/messaging/{{entity_singular}}_protocol.hpp"
<<paste:068323E2-D722-46F1-8B86-46BD32426003>>
#include <memory>

namespace ores::{{component}}::messaging {

namespace {
static constexpr std::string_view queue_group = "ores.{{component}}.service";
} // namespace

std::vector<ores::nats::service::subscription>
register_{{entity_singular}}_handlers(ores::nats::service::client& nats,
                                       ores::database::context ctx,
                                       std::optional<ores::security::jwt::jwt_authenticator> verifier) {
    std::vector<ores::nats::service::subscription> subs;
    auto h = std::make_shared<{{entity_singular}}_handler>(nats, std::move(ctx), std::move(verifier));
{{#operations}}
    subs.push_back(nats.queue_subscribe(
        {{request}}::nats_subject, queue_group,
        [h](ores::nats::message msg) { h->{{method}}(std::move(msg)); }));
{{/operations}}
<<paste:FB267332-79AB-4873-AFDA-CEEE68A0F398>>
    return subs;
}

} // namespace ores::{{component}}::messaging
{{/domain_entity}}
{{#junction}}
#include "ores.{{component_core}}/messaging/{{name_singular}}_registrar.hpp"
#include "ores.{{component_core}}/messaging/{{name_singular}}_handler.hpp"
#include "ores.{{component_include}}/messaging/{{name_singular}}_protocol.hpp"
#include <memory>

namespace ores::{{component}}::messaging {

namespace {
static constexpr std::string_view queue_group = "ores.{{component}}.service";
} // namespace

std::vector<ores::nats::service::subscription>
register_{{name_singular}}_handlers(ores::nats::service::client& nats,
                                     ores::database::context ctx,
                                     std::optional<ores::security::jwt::jwt_authenticator> verifier) {
    std::vector<ores::nats::service::subscription> subs;
    auto h = std::make_shared<{{name_singular}}_handler>(nats, std::move(ctx), std::move(verifier));
{{! A read-only junction derives no write verb, so this one loop states none. }}
{{#operations}}
    subs.push_back(nats.queue_subscribe(
        {{request}}::nats_subject, queue_group,
        [h](ores::nats::message msg) { h->{{method}}(std::move(msg)); }));
{{/operations}}
    return subs;
}

} // namespace ores::{{component}}::messaging
{{/junction}}

2. See also

Emacs 29.3 (Org mode 9.6.15)