Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vbrandl/actix-request-identifier
Middleware for actix-web to associate an ID with each request
https://github.com/vbrandl/actix-request-identifier
actix-web middleware request-id
Last synced: 4 days ago
JSON representation
Middleware for actix-web to associate an ID with each request
- Host: GitHub
- URL: https://github.com/vbrandl/actix-request-identifier
- Owner: vbrandl
- License: apache-2.0
- Created: 2020-04-20T11:34:52.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-10-08T09:22:25.000Z (about 1 month ago)
- Last Synced: 2024-11-09T01:52:25.409Z (11 days ago)
- Topics: actix-web, middleware, request-id
- Language: Rust
- Homepage:
- Size: 83 KB
- Stars: 8
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Actix Request Identifier Middleware
[![Latest Version](https://img.shields.io/crates/v/actix-request-identifier.svg)](https://crates.io/crates/actix-request-identifier)
[![Documentation](https://docs.rs/actix-request-identifier/badge.svg)](https://docs.rs/actix-request-identifier)
![Rust](https://github.com/vbrandl/actix-request-identifier/workflows/Rust/badge.svg)
[![Hits-of-Code](https://hitsofcode.com/github/vbrandl/actix-request-identifier)](https://hitsofcode.com/view/github/vbrandl/actix-request-identifier)
[![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/vbrandl/merging-iterator/blob/master/LICENSE-MIT)
[![License](https://img.shields.io/badge/license-Apache-green.svg)](https://github.com/vbrandl/merging-iterator/blob/master/LICENSE-APACHE)`actix-request-identifier` implements a middleware for
[`actix-web`](https://github.com/actix/actix-web) that generates an ID for each
incomming request. By default, a UUID v4 is generated for each request using
[`uuid`](https://github.com/uuid-rs/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`.## Example
```rust
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 GMT5f099854-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](https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-04.html#name-uuid-version-7).## Supported `actix-web` Versions
| crate version | `actix-web` version |
| ------------- | ------------------- |
| `0.1.0` | `v2` |
| `0.2.0` | `v3` |
| `4.0.0` | `v4` |## License
`actix-request-identifier` is licensed under either of the following at your
option:- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)