actix-web-correlation-id

Crates.ioactix-web-correlation-id
lib.rsactix-web-correlation-id
version1.0.0
sourcesrc
created_at2021-03-10 16:08:15.568512
updated_at2023-08-01 08:35:57.755073
descriptionAn Actix-web middleware component which synchronises a correlation ID for cross API request logging
homepage
repositoryhttp://github.com/nigma143/actix-web-correlation-id
max_upload_size
id366832
size74,207
(nigma143)

documentation

http://docs.rs/actix-web-correlation-id/

README

Documentation crates.io

actix-web-correlation-id

An Actix-web middleware component which synchronises a correlation ID for cross API request logging

Example:

use actix_web::{
    client::Client,
    middleware::Logger,
    web::{self},
    App, Error, HttpResponse, HttpServer,
};
use actix_web_correlation_id::{
    Correlation, CorrelationId, CorrelationIdPropagate, CorrelationIdVariable,
};

async fn index(corr_id: CorrelationId) -> Result<HttpResponse, Error> {
    let client = Client::new();

    let mut res = client
        .get("http://google.com/")
        .with_corr_id(corr_id)
        .send()
        .await?;

    let mut client_resp = HttpResponse::build(res.status());

    Ok(client_resp.body(res.body().await?))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));

    HttpServer::new(move || {
        App::new()
            .wrap(
                Logger::new("%{corr-id}xi %a \"%r\" %s %b \"%{Referer}i\" \"%{User-Agent}i\" %T")
                    .add_corr_id(),
            )
            .wrap(
                Correlation::new()
                    .header_name("x-correlation-id")
                    .enforce_header(false)
                    .resp_header_name(Some("x-correlation-id"))
                    .include_in_resp(true),
            )
            .service(web::resource("/simple").route(web::post().to(index)))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}
Commit count: 0

cargo fmt