Reinhardt Dispatch
HTTP request dispatching and handler system for the Reinhardt framework.
Overview
reinhardt-dispatch provides the core request handling functionality, equivalent to Django's django.core.handlers and django.dispatch. It orchestrates the complete request lifecycle including middleware execution, signal emission, and exception handling.
Installation
Add reinhardt to your Cargo.toml:
[]
= { = "0.1.0-rc.30", = ["dispatch"] }
# Or use a preset:
# reinhardt = { version = "0.1.0-rc.30", features = ["standard"] } # Recommended
# reinhardt = { version = "0.1.0-rc.30", features = ["full"] } # All features
Then import dispatch features:
use ;
Note: Dispatch features are included in the standard and full feature presets.
Features
- Request Lifecycle Management: Handle HTTP requests from start to finish
- Middleware Chain: Composable middleware for request/response processing
- Signal Integration: Emit lifecycle signals (
request_started,request_finished) - Exception Handling: Convert errors into appropriate HTTP responses
- Async Support: Full async/await support with Tokio
Architecture
Request → BaseHandler → Middleware Chain → URL Resolver → View → Response
↓ ↓
Signals Signals
(request_started) (request_finished)
Usage
Basic Request Handling
use ;
use ;
async
With Middleware
use ;
use ;
use Arc;
async
Exception Handling
The exception handler automatically converts errors into HTTP responses:
DispatchError::View→ 500 Internal Server ErrorDispatchError::UrlResolution→ 404 Not FoundDispatchError::Middleware→ 500 Internal Server ErrorDispatchError::Http→ 400 Bad RequestDispatchError::Internal→ 500 Internal Server Error
Components
BaseHandler
The core request handler that:
- Emits
request_startedsignal - Processes the request (delegates to URL resolver and views)
- Emits
request_finishedsignal - Handles exceptions
MiddlewareChain
Composes multiple middleware components into a processing pipeline:
let chain = new
.add_middleware?
.add_middleware?
.build;
Middleware are executed in reverse order (LIFO), so the last middleware added is the first to process the request.
Dispatcher
High-level dispatcher that coordinates between the handler and the rest of the framework:
use Dispatcher;
let dispatcher = new;
let response = dispatcher.dispatch.await?;
Exception Handling
The exception module provides:
ExceptionHandlertrait for custom exception handlingconvert_exception_to_responsehelper functionIntoResponsetrait for converting types to HTTP responses
Django Equivalents
| Reinhardt | Django |
|---|---|
BaseHandler |
django.core.handlers.base.BaseHandler |
MiddlewareChain |
django.core.handlers.base.MiddlewareChain |
ExceptionHandler |
django.core.handlers.exception.exception_handler |
request_started signal |
django.core.signals.request_started |
request_finished signal |
django.core.signals.request_finished |
Implementation Notes
This crate focuses on HTTP request dispatching, while signal dispatching is handled by reinhardt-core's signals module. This separation provides:
- Clear responsibility boundaries
- Independent signal system that can be used outside HTTP context
- Specialized HTTP request handling with middleware and exception handling
License
Licensed under the same terms as the Reinhardt project.