| Crates.io | actix-web-request-uuid |
| lib.rs | actix-web-request-uuid |
| version | 0.2.0 |
| created_at | 2025-05-25 04:54:06.161735+00 |
| updated_at | 2025-05-25 07:27:05.719532+00 |
| description | Request UUID middleware for actix-web |
| homepage | https://github.com/YusukeYoshida8849/actix-web-request-uuid |
| repository | https://github.com/YusukeYoshida8849/actix-web-request-uuid |
| max_upload_size | |
| id | 1688018 |
| size | 89,636 |
A Rust library to add request ID functionality with the actix-web framework.
Please refer to README.ja.md .
Add this to your Cargo.toml:
[dependencies]
actix-web-request-uuid = "0.1.0"
Add this to your crate root:
use actix_web::{web, App, HttpServer, HttpResponse, Error};
use actix_web_request_uuid::RequestIDMiddleware;
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.wrap(RequestIDMiddleware::new())
.service(web::resource("/").to(|| HttpResponse::Ok()))
})
.bind("127.0.0.1:59880")?
.run()
.await
}
This project is based on pastjean/actix-web-requestid with significant feature enhancements:
Added request ID management functionality using Thread-Local variables that the original project lacked:
get_current_request_id(): Access request ID from anywhere during request processingset_current_request_id(): Manually set request IDclear_current_request_id(): Clear request ID information// Get request ID from any function
async fn my_service() -> Result<(), Error> {
if let Some(request_id) = get_current_request_id() {
log::info!("Processing request: {}", request_id);
}
Ok(())
}
While the original project only supported UUID v4, this version supports various formats:
with_id_length() - Specify any length from 1 to 36 characterswith_full_uuid() - 36 characters with hyphenswith_simple_uuid() - 32 characters without hyphenswith_custom_uuid_format() - Custom formattersgenerator() - Completely custom ID generation logicheader_name() - Change default request-id// Configuration example
let middleware = RequestIDMiddleware::new()
.with_simple_uuid()
.header_name("X-Request-ID")
.generator(|| format!("req-{}", Uuid::new_v4().simple()));
More intuitive API through FromRequest implementation that the original project lacked:
// Get request ID directly in handler functions
async fn show_id(request_id: RequestID) -> impl Responder {
format!("Your request ID: {}", request_id)
}
Trait for directly getting request ID from HttpMessage:
pub trait RequestIDMessage {
fn request_id(&self) -> RequestID;
}
// Usage example
fn my_middleware(req: &HttpRequest) {
let id = req.request_id(); // Direct access
}
More detailed test coverage than the original project:
These enhancements have significantly upgraded the original simple request ID generation middleware into an enterprise-level request tracking system supporting log collection, distributed tracing, and debugging assistance.
For detailed documentation and examples, please refer to the docs.rs page.
Contributions are welcome! Please feel free to submit a Pull Request. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
This project is licensed under either of
at your option.
This is a fork of pastjean/actix-web-requestid. We thank the original author for their contribution to the Rust community.