]> piware.de Git - learn-rust.git/commitdiff
warp-server: Add User-Agent header filter
authorMartin Pitt <martin@piware.de>
Tue, 1 Nov 2022 15:30:53 +0000 (16:30 +0100)
committerMartin Pitt <martin@piware.de>
Tue, 1 Nov 2022 15:30:53 +0000 (16:30 +0100)
warp-server/src/main.rs

index 70b3cc3f516dbf3bfb622a7f2231435682b0d609..a89c7a6fd205e4703d04ed7f38a9232799dbd69a 100644 (file)
@@ -4,7 +4,8 @@ use warp::Filter;
 async fn main() {
     // GET /hello/warp => 200 OK with body "Hello, warp!"
     let hello = warp::path!("hello" / String)
-        .map(|name| format!("Hello, {}!", name));
+        .and(warp::header::<String>("user-agent"))
+        .map(|name, agent| format!("Hello, {} from {}!", name, agent));
 
     warp::serve(hello)
         .run(([127, 0, 0, 1], 3030))