iron_requestid

Crates.ioiron_requestid
lib.rsiron_requestid
version0.1.1
sourcesrc
created_at2017-04-09 19:54:14.717438
updated_at2017-04-09 20:06:48.365546
descriptionIron middleware for generating unique IDs for requests
homepagehttps://github.com/DonaldWhyte/iron_requestid
repositoryhttps://github.com/DonaldWhyte/iron_requestid
max_upload_size
id10150
size5,668
Donald Whyte (DonaldWhyte)

documentation

https://docs.rs/iron_requestid

README

iron_requestid

Iron middleware for generating unique IDs for requests.

Build Status Docs

This crate uses UUID4 generated IDs for requests. After installing the middleware, request IDs can be accessed in a request handler like so:

fn request_handler(request: &mut Request) -> IronResult<Response> {
    // ...
    let request_id = request.extensions.get::<RequestId>().unwrap();
    // ...
}

There is an example service that installs this middleware and retrieves a request's ID. The code for this is in examples/service.rs. Run the example service and send it a request to test it:

cargo run --example service &
curl localhost:3000/foo

The example service's source code is also shown below:

extern crate iron;
extern crate iron_requestid;

use iron::prelude::*;
use iron::status::Status;
use iron_requestid::{RequestId, RequestIds};

fn main() {
    let mut chain = Chain::new(return_request_handler);

    let (request_ids_before, request_ids_after) = RequestIds::new();
    chain.link_before(request_ids_before);
    chain.link_after(request_ids_after);

    Iron::new(chain).http("127.0.0.1:3000").unwrap();
}

fn return_request_handler(request: &mut Request) -> IronResult<Response> {
    match request.extensions.get::<RequestId>() {
        Some(id) => Ok(Response::with((
            Status::Ok,
            id.to_string()))),
        None => Ok(Response::with((
            Status::InternalServerError,
            "could not get request ID")))
    }
}
Commit count: 9

cargo fmt