Expand description
Extra middleware and utilities for the Salvo web framework.
This crate provides a collection of commonly-needed web features as middleware and utilities that complement Salvo’s core functionality. All features are opt-in through feature flags, allowing you to include only what you need.
§Quick Start
Add salvo_extra to your Cargo.toml with the features you need:
[dependencies]
salvo_extra = { version = "0.88", features = ["basic-auth", "websocket"] }Or use the salvo umbrella crate which re-exports these features:
[dependencies]
salvo = { version = "0.88", features = ["basic-auth", "websocket"] }§Feature Categories
§Authentication & Security
| Feature | Description |
|---|---|
basic-auth | HTTP Basic Authentication (RFC 7617) |
force-https | Redirect HTTP requests to HTTPS |
§Request/Response Processing
| Feature | Description |
|---|---|
caching-headers | Add cache control headers |
size-limiter | Limit request body size |
timeout | Set request processing timeout |
trailing-slash | Normalize URL trailing slashes |
§Concurrency & Resource Management
| Feature | Description |
|---|---|
concurrency-limiter | Limit concurrent requests |
affix-state | Attach shared state to requests |
§Observability
| Feature | Description |
|---|---|
logging | Request/response logging |
request-id | Unique request ID generation |
§Real-time Communication
| Feature | Description |
|---|---|
sse | Server-Sent Events for streaming updates |
websocket | Full-duplex WebSocket connections |
§Error Handling & Integration
| Feature | Description |
|---|---|
catch-panic | Convert panics to error responses |
tower-compat | Use Tower middleware with Salvo |
§Usage Example
ⓘ
use salvo::prelude::*;
use salvo_extra::basic_auth::{BasicAuth, BasicAuthValidator};
use salvo_extra::logging::Logger;
use salvo_extra::timeout::Timeout;
use std::time::Duration;
// Apply multiple middleware
let router = Router::new()
.hoop(Logger::new())
.hoop(Timeout::new(Duration::from_secs(30)))
.push(
Router::with_path("api")
.hoop(BasicAuth::new(MyValidator))
.get(my_handler)
);Re-exports§
pub use tower_compat::TowerServiceCompat;tower-compatpub use tower_compat::TowerLayerCompat;tower-compat
Modules§
- affix_
state affix-state - Middleware for adding shared application state to the request context.
- basic_
auth basic-auth - Middleware for HTTP Basic Authentication.
- caching_
headers caching-headers - Middleware for handling ETag and Last-Modified headers.
- catch_
panic catch-panic - Middleware for catch panic in handlers.
- concurrency_
limiter concurrency-limiter - Middleware for limiting concurrency.
- force_
https force-https - Middleware force redirect to https.
- logging
logging - A simple logging middleware.
- request_
id request-id - Request id middleware.
- size_
limiter size-limiter - Middleware for limiting request size.
- sse
sse - Middleware for Server-Sent Events (SSE)
- timeout
timeout - Middleware for controlling requests timeout.
- tower_
compat tower-compat - Adapters for
tower::Layerandtower::Service. - trailing_
slash trailing-slash - Trailing slash middleware.
- websocket
websocket - WebSocket implementation.