Skip to content

Commit

Permalink
add dyn_reply example
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs committed Nov 14, 2019
1 parent 7c1433a commit 3c7fe2c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/dyn_reply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![deny(warnings)]
use warp::{http::StatusCode, Filter};

async fn dyn_reply(word: String) -> Result<Box<dyn warp::Reply>, warp::Rejection> {
if &word == "hello" {
//a cast is needed for now, see https://github.com/rust-lang/rust/issues/60424
Ok(Box::new("world") as Box<dyn warp::Reply>)
} else {
Ok(Box::new(StatusCode::BAD_REQUEST) as Box<dyn warp::Reply>)
}
}

#[tokio::main]
async fn main() {
let routes = warp::path::param().and_then(dyn_reply);

warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}

0 comments on commit 3c7fe2c

Please sign in to comment.