| 3 | |
| 4 | #[tokio::main] |
| 5 | async fn main() { |
| 6 | let directory = warp::path("static").and(warp::fs::dir("../../static/")); |
| 7 | |
| 8 | let opt_name = warp::path::param::<String>() |
| 9 | .map(Some) |
| 10 | .or_else(|_| async { |
| 11 | Ok::<(Option<String>,), std::convert::Infallible>((None,)) }); |
| 12 | let hello = warp::path("hello") |
| 13 | .and(opt_name) |
| 14 | .map(|name: Option<String>| { |
| 15 | let msg = format!( |
| 16 | "Hello, {}!", |
| 17 | name.unwrap_or("World".to_string())); |
| 18 | warp::reply::json(&json!({ "message": msg })) |
| 19 | }); |
| 20 | let root = warp::path::end() |
| 21 | .and(warp::get()) |
| 22 | .map(|| warp::reply::json(&json!({"message": "Hello, World?"}))); |
| 23 | |
| 24 | let items = warp::path!("items" / i32) |
| 25 | .map(|id| warp::reply::json(&json!({ "item_id": id }))); |
| 26 | |
| 27 | let routes = directory.or(hello).or(items).or(root); |
| 28 | |
| 29 | println!("Serving on localhost:3030"); |
| 30 | warp::serve(routes).run(([127, 0, 0, 1], 3030)).await; |
| 31 | } |