Skip to main content

Crate salvo_extra

Crate salvo_extra 

Source
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

FeatureDescription
basic-authHTTP Basic Authentication (RFC 7617)
force-httpsRedirect HTTP requests to HTTPS

§Request/Response Processing

FeatureDescription
caching-headersAdd cache control headers
size-limiterLimit request body size
timeoutSet request processing timeout
trailing-slashNormalize URL trailing slashes

§Concurrency & Resource Management

FeatureDescription
concurrency-limiterLimit concurrent requests
affix-stateAttach shared state to requests

§Observability

FeatureDescription
loggingRequest/response logging
request-idUnique request ID generation

§Real-time Communication

FeatureDescription
sseServer-Sent Events for streaming updates
websocketFull-duplex WebSocket connections

§Error Handling & Integration

FeatureDescription
catch-panicConvert panics to error responses
tower-compatUse 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-compat
pub use tower_compat::TowerLayerCompat;tower-compat

Modules§

affix_stateaffix-state
Middleware for adding shared application state to the request context.
basic_authbasic-auth
Middleware for HTTP Basic Authentication.
caching_headerscaching-headers
Middleware for handling ETag and Last-Modified headers.
catch_paniccatch-panic
Middleware for catch panic in handlers.
concurrency_limiterconcurrency-limiter
Middleware for limiting concurrency.
force_httpsforce-https
Middleware force redirect to https.
logginglogging
A simple logging middleware.
request_idrequest-id
Request id middleware.
size_limitersize-limiter
Middleware for limiting request size.
ssesse
Middleware for Server-Sent Events (SSE)
timeouttimeout
Middleware for controlling requests timeout.
tower_compattower-compat
Adapters for tower::Layer and tower::Service.
trailing_slashtrailing-slash
Trailing slash middleware.
websocketwebsocket
WebSocket implementation.