Created
September 19, 2020 07:30
-
-
Save Szymongib/c051c56e8eba8f6464a448e809c370c0 to your computer and use it in GitHub Desktop.
Filter to log request headers using Warp framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [dependencies] | |
// tokio = { version = "0.2", features = ["macros"] } | |
// warp = "0.2.2" | |
use warp::Filter; | |
use warp::http::HeaderMap; | |
use std::convert::Infallible; | |
#[tokio::main] | |
async fn main() { | |
let port: u16 = 8080; | |
let api = warp::any() | |
.and(log_headers()) | |
.and_then(handle); | |
let server_future = warp::serve(api).run(([0, 0, 0, 0], port.clone())); | |
server_future.await; | |
} | |
fn log_headers() -> impl Filter<Extract = (), Error = Infallible> + Copy { | |
warp::header::headers_cloned() | |
.map(|headers: HeaderMap| { | |
for (k, v) in headers.iter() { | |
// Error from `to_str` should be handled properly | |
println!("{}: {}", k, v.to_str().expect("Failed to print header value")) | |
} | |
}) | |
.untuple_one() | |
} | |
async fn handle() -> Result<impl warp::Reply, warp::Rejection> { | |
Ok(warp::reply::reply()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment