Skip to content

Instantly share code, notes, and snippets.

@fteychene
Created March 5, 2021 15:13
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 fteychene/35a1ee3777159b86d246e0da011142c9 to your computer and use it in GitHub Desktop.
Save fteychene/35a1ee3777159b86d246e0da011142c9 to your computer and use it in GitHub Desktop.
Warp route test
use warp::{Filter, Rejection};
use warp::generic::Either;
fn other_routes() -> impl Filter<Error = Rejection, Extract = (Either<(Either<(String,), (String,)>,), (String,)>,)> + Clone + Send + Sync + 'static {
// GET /hi
let hi = warp::get().and(warp::path("hi").map(|| "Hello, World!".to_string()));
// GET /hello/from/warp
let hello_from_warp = warp::get().and(warp::path!("hello" / "from" / "warp").map(|| "Hello from warp!".to_string()));
// GET /sum/:u32/:u32
let sum = warp::get().and(warp::path!("sum" / u32 / u32).map(|a, b| format!("{} + {} = {}", a, b, a + b)));
hi.or(hello_from_warp).or(sum)
}
#[tokio::main]
async fn main() {
// GET /
let hello_world = warp::path::end().map(|| "Hello, World at root!");
let routes = warp::get().and(hello_world)
.or(other_routes());
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment