Expand description

Actix web middleware hook for request start and end. Subscribe to request start and end, request data, elapsed time, request id and response status.

Setup:

use std::rc::Rc;
use actix_web::{App, Error, HttpServer, web};
use actix_request_hook::observer::{Observer, RequestEndData, RequestStartData};
use actix_request_hook::RequestHook;
struct RequestLogger;

impl Observer for RequestLogger {
    fn on_request_started(&self, data: RequestStartData) {
        println!("started {}", data.uri)
    }

    fn on_request_ended(&self, data: RequestEndData) {
        println!("ended {} after {}ms", data.uri, data.elapsed.as_millis())
    }
}

async fn index() -> Result<String, Error> {
    Ok("Hi there!".to_string())
}


// You can register many different observers.
// One could be for logging, other for notifying other parts of the system etc.
let request_hook = RequestHook::new()
           .exclude("/bye") // bye route shouldn't be logged
           .exclude_regex("^/\\d$") // excludes any numbered route like "/123456"
           .register(Rc::new(RequestLogger{}));
App::new()
    .wrap(request_hook)
    .route("/bye", web::get().to(index))
    .route("/hey", web::get().to(index));

Modules

Observer trait and function implementations.

Structs

Middleware for subscribing to request start and end. Enables access to request data, id, status and request duration.