middleware — TutorialsThese tutorials build up a small Actix-Web service step by step, adding one middleware at a time. Each section assumes the previous ones are in place, so by the end you have a service with request tracing, JWT auth, rate limiting, pagination, and idempotent writes all working together, correctly ordered.
If you only need a single middleware in isolation, see EXAMPLES.md instead.
RequestIdRequestId is almost always the outermost middleware, because other pieces
(ReadContext<T>) depend on it having already run.
use actix_web::{web, App, HttpServer, HttpResponse};
use actixutils::middleware::RequestId;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(RequestId)
.route("/ping", web::get().to(ping))
})
.bind(("0.0.0.0", 8080))?
.run()
.await
}
async fn ping() -> HttpResponse {
HttpResponse::Ok().finish()
}
Run it and check the response headers — every response now carries an
X-Request-Id: <uuid> header, and every tracing span emitted while
handling the request includes the same id. Handlers can read it back with:
use actix_web::{HttpRequest, HttpMessage};
use actixutils::middleware::RequestIdStr;
async fn ping(req: HttpRequest) -> HttpResponse {
let rid = req.extensions().get::<RequestIdStr>().unwrap().0.clone();
tracing::info!(request_id = %rid, "handled ping");
HttpResponse::Ok().finish()
}
Auth<T>Next, protect an API scope with bearer-token auth. Auth<T> needs a
validator — anything implementing actixutils::locals::Validate<T>. We’ll use
the crate’s HS256Signer against an Identity claims type.
use actix_web::{web, App};
use actixutils::locals::{HS256Signer, Identity, Validate};
use actixutils::middleware::{Auth, RequestId};
use std::sync::Arc;
let signer: Arc<dyn Validate<Identity>> =
Arc::new(HS256Signer::new("my-service".to_string(), "super-secret".to_string()));
App::new()
.wrap(RequestId)
.service(
web::scope("/api")
.wrap(Auth { validator: signer.clone() })
.route("/me", web::get().to(me_handler)),
);
async fn me_handler(req: actix_web::HttpRequest) -> actix_web::HttpResponse {
use actix_web::HttpMessage;
let identity = req.extensions().get::<Identity>().cloned();
actix_web::HttpResponse::Ok().json(identity.is_some())
}
What happens on a request:
Auth<T> looks for the token in the Authorization: Bearer <token> header,
falling back to an access_token cookie.401 Unauthorized immediately.signer.validate(&token) fails → 401 Unauthorized, with the underlying
error logged via tracing::warn!.Identity (or whatever T you chose) is
inserted into request extensions for every downstream handler and
middleware to read.If a value of type T is already in extensions (e.g. inserted by an outer
middleware layer during testing), Auth<T> skips validation entirely — handy
for injecting a fake principal in integration tests without a real token.
RateLimiter<T>RateLimiter<T> needs a key type that implements both FromRequest (to pull
it out of the request) and locals::rate_limiter::GetId (to turn it into a
hashable key). Because both the trait and Jwt<Identity> belong to
actixutils, Rust’s orphan rule stops you implementing one for the other in your
own crate, so define a small local key type. This one reads the Identity that
Auth<Identity> already stored in the request extensions:
use actix_web::{error::ErrorUnauthorized, Error, FromRequest, HttpMessage, HttpRequest};
use actix_web::dev::Payload;
use actixutils::locals::{rate_limiter::GetId, Identity};
use std::future::{ready, Ready};
use uuid::Uuid;
struct UserKey(Uuid);
impl FromRequest for UserKey {
type Error = Error;
type Future = Ready<Result<Self, Error>>;
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
ready(
req.extensions()
.get::<Identity>()
.map(|id| UserKey(id.sub))
.ok_or_else(|| ErrorUnauthorized("not authenticated")),
)
}
}
impl GetId for UserKey {
type Id = Uuid;
fn id(&self) -> Uuid {
self.0
}
}
RateLimiter::new also needs a backing store: an
Arc<dyn ferrumec::Store<Uuid, VecDeque<Instant>>> here, since UserKey::Id
is Uuid. actixutils doesn’t ship an in-memory Store, so supply a minimal one
(a HashMap behind a lock is enough for a single process; see
src/middleware/test_session.rs for the same pattern used with sessions):
use async_trait::async_trait;
use ferrumec::Store;
use std::{
collections::{HashMap, VecDeque},
error::Error,
sync::Arc,
time::Instant,
};
use tokio::sync::RwLock;
use uuid::Uuid;
struct MemoryRateStore {
inner: RwLock<HashMap<Uuid, VecDeque<Instant>>>,
}
#[async_trait]
impl Store<Uuid, VecDeque<Instant>> for MemoryRateStore {
async fn get(&self, key: &Uuid) -> Result<Option<VecDeque<Instant>>, Box<dyn Error>> {
Ok(self.inner.read().await.get(key).cloned())
}
async fn set(&self, key: &Uuid, value: VecDeque<Instant>) -> Result<(), Box<dyn Error>> {
self.inner.write().await.insert(*key, value);
Ok(())
}
async fn delete(&self, key: &Uuid) -> Result<(), Box<dyn Error>> {
self.inner.write().await.remove(key);
Ok(())
}
async fn clear(&self) -> Result<(), Box<dyn Error>> {
self.inner.write().await.clear();
Ok(())
}
}
Then wrap the scope. UserKey reads the identity from extensions, so
RateLimiter has to run after Auth. Actix runs the last .wrap() first, so
register RateLimiter before Auth:
use actixutils::middleware::RateLimiter;
use std::time::Duration;
let rate_store = Arc::new(MemoryRateStore { inner: RwLock::new(HashMap::new()) });
web::scope("/api")
.wrap(RateLimiter::<UserKey>::new(rate_store, 100, Duration::from_secs(60))) // runs second
.wrap(Auth { validator: signer.clone() }) // runs first
.route("/me", web::get().to(me_handler));
This allows 100 requests per user per rolling 60-second window. Requests over
the limit get 429 Too Many Requests without the handler ever running. The
MemoryRateStore above is per-process, which is fine for a single instance; for
multi-node deployments implement Store against a shared backend such as Redis.
Heads up: if the key extractor fails,
RateLimiterdoes not reject the request; it lets it through unlimited. HereAuthrejects unauthenticated requests first, so that never happens, but keep it in mind when keying on something that can be absent.
Idempotency<Store>For any POST/PUT/PATCH endpoint where a network retry could double-apply a
mutation (charging a card, creating an order), wrap it in Idempotency.
You need a store. Note that locals::IdempotencyStore is its own trait
family (acquire/get/complete/release), separate from the general
ferrumec::Store<K, V> trait used by RateLimiter and the session
middleware. It isn’t a Store implementation. For a quick start, an in-memory one:
use actixutils::locals::{IdempotencyStore, IdempotencyState, CachedResponse};
use async_trait::async_trait;
use std::{collections::HashMap, sync::{Arc, Mutex}, time::Duration};
#[derive(Default)]
struct MemoryIdempotencyStore {
entries: Mutex<HashMap<String, IdempotencyState>>,
}
#[async_trait]
impl IdempotencyStore for MemoryIdempotencyStore {
type Error = std::convert::Infallible;
async fn acquire(&self, key: &str, _ttl: Duration) -> Result<bool, Self::Error> {
let mut entries = self.entries.lock().unwrap();
if entries.contains_key(key) {
Ok(false)
} else {
entries.insert(key.to_string(), IdempotencyState::InProgress);
Ok(true)
}
}
async fn get(&self, key: &str) -> Result<Option<IdempotencyState>, Self::Error> {
Ok(match self.entries.lock().unwrap().get(key) {
Some(IdempotencyState::InProgress) => Some(IdempotencyState::InProgress),
Some(IdempotencyState::Completed(r)) => Some(IdempotencyState::Completed(r.clone())),
None => None,
})
}
async fn complete(&self, key: &str, response: CachedResponse) -> Result<(), Self::Error> {
self.entries
.lock()
.unwrap()
.insert(key.to_string(), IdempotencyState::Completed(response));
Ok(())
}
async fn release(&self, key: &str) -> Result<(), Self::Error> {
self.entries.lock().unwrap().remove(key);
Ok(())
}
}
acquire is atomic here because the check and the insert happen under one
Mutex guard, but this store ignores the TTL and never evicts, and it only
works within one process. For production, back it with Redis
(SET key val NX EX ttl) or an equivalent atomic primitive.
Wire it into the payments scope:
use actixutils::middleware::Idempotency;
let store = Arc::new(MemoryIdempotencyStore::default());
web::scope("/payments")
.wrap(Idempotency::new(store).ttl(Duration::from_secs(24 * 60 * 60)))
.route("/charge", web::post().to(charge_handler));
Now a client that wants a charge to be retry-safe sends an
Idempotency-Key: <client-generated-uuid> header. The first request with a
given key runs the handler normally and caches the resulting status, headers,
and body. Any subsequent request with the same key — while the first is still
in flight, or after it completed — gets the cached outcome instead of
re-running charge_handler. A repeat that arrives while the first is still
running gets 409 Conflict. Every completed response is cached, including
error responses, so a client that wants to retry after a failure must use a
new key. Requests with no Idempotency-Key header pass through untouched, so
this is safe to apply broadly.
PaginationMiddlewareFor list endpoints, avoid passing page/limit through every function
signature by reading them from a task-local instead.
use actixutils::middleware::PaginationMiddleware;
use actixutils::locals::Pagination;
web::scope("/items")
.wrap(PaginationMiddleware)
.route("", web::get().to(list_items));
async fn list_items() -> actix_web::HttpResponse {
let items = repo::list().await; // repo::list reads Pagination::get() internally, several calls deep
actix_web::HttpResponse::Ok().json(items)
}
mod repo {
use actixutils::locals::Pagination;
pub async fn list() -> Vec<String> {
let p = Pagination::get();
// SELECT ... LIMIT p.limit OFFSET (p.page * p.limit)
vec![]
}
}
?page= and ?limit= are optional; missing or unparseable values default to
page = 0, limit = 100. Because the value lives in a Tokio task-local
scoped around the whole request future, repo::list() can call
Pagination::get() without list_items ever seeing a Pagination value
itself.
A full stack combining everything above, respecting dependency order (outermost first):
App::new()
.wrap(RequestId) // 1. correlation id, outermost
.service(
web::scope("/api")
.wrap(Auth { validator: signer.clone() }) // 2. authenticate, inserts Identity
.service(
web::scope("/items")
.wrap(PaginationMiddleware) // 5. list params
.wrap(RateLimiter::<UserKey>::new(rate_store.clone(), 100, Duration::from_secs(60))) // 3. per-user limit
.route("", web::get().to(list_items))
.route("", web::post().to(create_item)),
)
.service(
web::scope("/payments")
.wrap(Idempotency::new(idempotency_store.clone())) // 4. retry-safety
.route("/charge", web::post().to(charge_handler)),
),
);
Auth sits on the outer /api scope, so by the time the inner scopes’
middleware run the Identity is already in the request extensions.
From here, see EXAMPLES.md for standalone snippets covering
ResponseEqualizer, Session<T>/RequiredSession<T>, ReadContext<T>,
AttachLocal<T>, PathParams, Singleflight<K, KeyFn>, TimeoutMiddleware,
ClientIpMiddleware, and the identity/authority helper functions that
weren’t needed in this walkthrough.