Skip to main content

Module microsvc

Module microsvc 

Source
Expand description

microsvc — Convention-based microservice command handler framework.

Build microservices by registering command and event handlers on a Service. Each handler receives a Context<D> with access to the input payload, session variables, and the service dependencies.

§Quick Start

use std::sync::Arc;
use distributed::{microsvc, HashMapRepository};
use serde_json::json;

let service = Arc::new(
    microsvc::Service::new().with_repo(HashMapRepository::new())
        .command("order.create")
        .handle(|ctx| {
            let input = ctx.input::<CreateOrderInput>()?;
            Ok(json!({ "id": input.id }))
        })
);

// Direct dispatch
let result = service.dispatch("order.create", json!({ "id": "o1" }), microsvc::Session::new());

// HTTP transport (requires "http" feature)
// microsvc::serve(service, "0.0.0.0:3000").await?;

§Handler Convention

Each handler file follows this convention:

// src/handlers/order_create.rs

pub const COMMAND: &str = "order.create";

pub fn guard<D>(ctx: &microsvc::Context<D>) -> bool {
    ctx.has_fields(&["id", "product_id"])
}

pub fn handle<D>(ctx: &microsvc::Context<D>) -> Result<Value, microsvc::HandlerError>
where
    D: microsvc::HasRepo,
    D::Repo: CommitAggregate,
{
    let input = ctx.input::<CreateOrderInput>()?;
    let mut order = Order::default();
    order.create(input.id);
    ctx.repo().commit_aggregate(&mut order)?;
    Ok(json!({ "id": order.entity().id() }))
}

Re-exports§

pub use crate::bus::Message;
pub use crate::bus::MessageKind;
pub use crate::bus::PayloadDecodeError;
pub use crate::bus::SubscriptionPlan;

Structs§

CommandRequest
An inbound command request.
CommandResponse
Response from dispatching a command.
Context
The context passed to every handler.
HandlerBuilder
Builder returned by Service::command, Service::event, Service::events, and Service::handler.
HandlerSpec
Transport-visible metadata for a registered handler.
ReadModelStoreDependencies
Dependencies for a service that only needs a read-model store.
RepoDependencies
Dependencies for a service that only needs an aggregate repository.
RepoReadModelDependencies
Dependencies for a service that needs both an aggregate repository and a read-model store.
Service
A microservice that routes commands to handler functions.
Session
Parsed session variables from the incoming request.

Enums§

DeliveryKind
How a handler expects the transport to deliver matching messages.
HandlerError
Error type for command handler operations.
HandlerNames
Static message names attached to a handler spec.

Constants§

DEFAULT_MAX_PUBLISH_ATTEMPTS
Default publish-failure ceiling before an outbox row is permanently failed.
DEFAULT_PUBLISH_LEASE
Default lease for an immediate after-commit outbox publish. Short by design: it only needs to cover commit → publish, so a crash before the publish completes hands the row back to the polling worker quickly.

Traits§

ConfigurableOutboxPublisher
Dependency capability for repositories whose outbox commits can publish immediately.
HasOutboxStore
Dependency capability for repositories that expose a durable outbox store.
HasReadModelStore
Dependency capability for services that expose a read-model store.
HasRepo
Dependency capability for services that expose an aggregate repository.