Skip to content

Instantly share code, notes, and snippets.

@udoprog
Created September 30, 2019 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udoprog/8a8ae0d188612ef29754f92e3959b35d to your computer and use it in GitHub Desktop.
Save udoprog/8a8ae0d188612ef29754f92e3959b35d to your computer and use it in GitHub Desktop.
use futures::Future;
use warp::Filter;
#[tokio::main]
async fn main() {
let r1 = warp::path("index").and_then(route1);
let r2 = warp::path::param().and_then(route2);
let routes = r1.recover(recover).or(r2.recover(recover));
warp::serve(routes).run(([127, 0, 0, 1], 3020)).await;
}
fn route1() -> impl Future<Output = Result<impl warp::Reply, warp::Rejection>> {
dbg!("was here");
futures::future::err::<String, warp::Rejection>(warp::reject::custom(
std::io::Error::from(std::io::ErrorKind::ConnectionAborted),
))
}
fn route2(_param: String) -> impl Future<Output = Result<impl warp::Reply, warp::Rejection>> {
futures::future::ok("hello world")
}
async fn recover(err: warp::Rejection) -> Result<impl warp::Reply, warp::Rejection> {
Ok(warp::reply::with_status("an error happened", warp::http::StatusCode::BAD_REQUEST))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment