https://github.com/doumanash/tower-http-req-id
Tower middleware to generate and make use of request id
https://github.com/doumanash/tower-http-req-id
Last synced: 5 months ago
JSON representation
Tower middleware to generate and make use of request id
- Host: GitHub
- URL: https://github.com/doumanash/tower-http-req-id
- Owner: DoumanAsh
- License: bsl-1.0
- Created: 2021-09-01T06:47:36.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-05-12T13:48:36.000Z (over 2 years ago)
- Last Synced: 2025-04-08T11:18:52.241Z (9 months ago)
- Language: Rust
- Size: 10.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tower-http-req-id
[](https://crates.io/crates/tower-http-req-id)
[](https://docs.rs/crate/tower-http-req-id/)
[](https://github.com/DoumanAsh/tower-http-req-id/actions?query=workflow%3ARust)
Tower middleware to generate and make use of request id.
This middleware checks if request has `x-request-id` set by user and adds it the extension.
Note that if header's value is not valid unicode string, then it is considered non-existing.
If it is not present or invalid value for this type of ID, then automatically generates using specified generator.
To cover as many strategies as possible, it is best to use `String` type that can accept any type of id from client.
## Features:
- `uuid` - Enables UUID based generator.
## Defining own ID generator:
```rust
use tower_http_req_id::{IdGen, GenerateRequestIdLayer};
//Clone trait is necessary to fit tower's bounds
#[derive(Clone)]
struct TestGenerator;
impl IdGen for TestGenerator {
#[inline(always)]
fn gen(&self) -> String {
"whatever".to_owned()
}
}
//First type parameter is generator, which can be determined from `new` function.
//Second type parameter is ID's type, which must be always specified manually.
let generator = GenerateRequestIdLayer::<_, String>::new(TestGenerator);
```
## Accessing ID:
ID is stored within `request` extensions map, which can be accessed by id's type.