Crates.io | actix-request-identifier |
lib.rs | actix-request-identifier |
version | 4.2.0 |
source | src |
created_at | 2020-04-20 14:24:03.098749 |
updated_at | 2024-04-13 10:41:37.215543 |
description | Middlerware for actix-web to associate an ID with each request. |
homepage | https://github.com/vbrandl/actix-request-identifier |
repository | https://github.com/vbrandl/actix-request-identifier |
max_upload_size | |
id | 232214 |
size | 67,597 |
actix-request-identifier
implements a middleware for
actix-web
that generates an ID for each
incomming request. By default, a UUID v4 is generated for each request using
uuid
. The request ID is set via the
x-request-id
HTTP header (which can be configured) and can be extracted from
the request using RequestId
.
use actix_request_identifier::{RequestId, RequestIdentifier};
use actix_web::{get, App, HttpServer, Responder};
#[get("/")]
async fn show_request_id(id: RequestId) -> impl Responder {
format!("{}", id.as_str())
}
#[actix_web::main] // or #[tokio::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(show_request_id)
.wrap(RequestIdentifier::with_uuid())
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
The response looks like this:
$ curl -i 127.0.0.1:8080/
HTTP/1.1 200 OK
content-length: 36
x-request-id: 5f099854-2117-49b3-b252-d6693a85acc5
date: Mon, 20 Apr 2020 06:53:49 GMT
5f099854-2117-49b3-b252-d6693a85acc5
If you want chronologically sortable request IDs, you can enable the
uuid-v7-generator
feature and RequestIdentifier::with_uuid_v7()
. This will
generate request IDs using UUID Version
7.
actix-web
Versionscrate version | actix-web version |
---|---|
0.1.0 |
v2 |
0.2.0 |
v3 |
4.0.0 |
v4 |
actix-request-identifier
is licensed under either of the following at your
option: