Skip to content

Commit fbb74f8

Browse files
committed
add dyn_reply example
1 parent 7c1433a commit fbb74f8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

examples/dyn_reply.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![deny(warnings)]
2+
use warp::{http::StatusCode, Filter};
3+
4+
async fn dyn_reply(word: String) -> Result<Box<dyn warp::Reply>, warp::Rejection> {
5+
if &word == "hello" {
6+
Ok(Box::new("world") as Box<dyn warp::Reply>)
7+
} else {
8+
Ok(Box::new(StatusCode::BAD_REQUEST) as Box<dyn warp::Reply>)
9+
}
10+
}
11+
12+
#[tokio::main]
13+
async fn main() {
14+
let routes = warp::path::param().and_then(dyn_reply);
15+
16+
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
17+
}

0 commit comments

Comments
 (0)