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: µsvc::Context<D>) -> bool {
ctx.has_fields(&["id", "product_id"])
}
pub fn handle<D>(ctx: µsvc::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§
- Command
Request - An inbound command request.
- Command
Response - Response from dispatching a command.
- Context
- The context passed to every handler.
- Handler
Builder - Builder returned by
Service::command,Service::event,Service::events, andService::handler. - Handler
Spec - Transport-visible metadata for a registered handler.
- Read
Model Store Dependencies - Dependencies for a service that only needs a read-model store.
- Repo
Dependencies - Dependencies for a service that only needs an aggregate repository.
- Repo
Read Model Dependencies - 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§
- Delivery
Kind - How a handler expects the transport to deliver matching messages.
- Handler
Error - Error type for command handler operations.
- Handler
Names - 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§
- Configurable
Outbox Publisher - Dependency capability for repositories whose outbox commits can publish immediately.
- HasOutbox
Store - Dependency capability for repositories that expose a durable outbox store.
- HasRead
Model Store - Dependency capability for services that expose a read-model store.
- HasRepo
- Dependency capability for services that expose an aggregate repository.