Middleware
Middleware is the layer in Postboy that allows you to observe, influence, and control message processing in a centralized way.
It is designed for cross-cutting concerns — the kind of logic that should not live inside every single handler or executor.
What middleware is for
Middleware is useful when you want to handle things like:
logging
validation
tracing
metrics
access control
cancellation
payload inspection
flow decisions
Instead of repeating this logic across the application, you can place it in a dedicated processing layer.
Why it matters
As an application grows, shared concerns tend to spread across the codebase. That makes the system harder to maintain and reason about.
Middleware helps keep that logic in one place, so the core message handlers stay focused on business behavior.
How middleware works in Postboy
In Postboy, middleware participates in the processing pipeline around different stages of the flow.
A typical middleware flow looks like this:
a message or executor enters the pipeline
middleware runs before the main action
the action is processed
middleware runs after the action
the result is returned or the flow continues
This gives Postboy a controlled and extensible way to manage message lifecycle behavior.
Key characteristics
Middleware in Postboy is typically:
centralized — shared logic lives in one place
stage-aware — it can react to different phases of the flow
typed — it works with the message context in a structured way
controllable — it can allow, modify, or interrupt processing
isolated — it does not need to be mixed into handlers directly
When to use middleware
Use middleware when you want to:
check whether a message should continue
add logging or tracing around message handling
validate message content before processing
collect metrics about message flow
enforce rules consistently across the bus
Middleware vs handler logic
A handler should focus on what to do with a message.
Middleware should focus on what should happen around that message.
That separation helps keep the architecture cleaner and the business logic easier to test.
In short
Middleware is Postboy’s way of keeping shared processing logic out of the core handlers and executors.
It makes the event flow more structured, predictable, and easier to maintain.