| 1 |
#![deny(warnings)] |
| 2 |
|
| 3 |
use warp::Filter; |
| 4 |
|
| 5 |
#[tokio::main] |
| 6 |
async fn main() { |
| 7 |
pretty_env_logger::init(); |
| 8 |
|
| 9 |
// We'll start simple, and gradually show how you combine these powers |
| 10 |
// into super powers! |
| 11 |
|
| 12 |
// GET / |
| 13 |
let hello_world = warp::path::end().map(|| "Hello, World at root!"); |
| 14 |
|
| 15 |
// GET /hi |
| 16 |
let hi = warp::path("hi").map(|| "Hello, World!"); |
| 17 |
|
| 18 |
// How about multiple segments? First, we could use the `path!` macro: |
| 19 |
// |
| 20 |
// GET /hello/from/warp |
| 21 |
let hello_from_warp = warp::path!("hello" / "from" / "warp").map(|| "Hello from warp!"); |
| 22 |
|
| 23 |
// Fine, but how do I handle parameters in paths? |
| 24 |
// |
| 25 |
// GET /sum/:u32/:u32 |
| 26 |
let sum = warp::path!("sum" / u32 / u32).map(|a, b| format!("{} + {} = {}", a, b, a + b)); |
| 27 |
|
| 28 |
// Any type that implements FromStr can be used, and in any order: |
| 29 |
// |
| 30 |
// GET /:u16/times/:u16 |
| 31 |
let times = |
| 32 |
warp::path!(u16 / "times" / u16).map(|a, b| format!("{} times {} = {}", a, b, a * b)); |
| 33 |
|
| 34 |
// Oh shoot, those math routes should be mounted at a different path, |
| 35 |
// is that possible? Yep. |
| 36 |
// |
| 37 |
// GET /math/sum/:u32/:u32 |
| 38 |
// GET /math/:u16/times/:u16 |
| 39 |
let math = warp::path("math"); |
| 40 |
let _sum = math.and(sum); |
| 41 |
let _times = math.and(times); |
| 42 |
|
| 43 |
// What! And? What's that do? |
| 44 |
// |
| 45 |
// It combines the filters in a sort of "this and then that" order. In |
| 46 |
// fact, it's exactly what the `path!` macro has been doing internally. |
| 47 |
// |
| 48 |
// GET /bye/:string |
| 49 |
let bye = warp::path("bye") |
| 50 |
.and(warp::path::param()) |
| 51 |
.map(|name: String| format!("Good bye, {}!", name)); |
| 52 |
|
| 53 |
// Ah, can filters do things besides `and`? |
| 54 |
// |
| 55 |
// Why, yes they can! They can also `or`! As you might expect, `or` creates |
| 56 |
// a "this or else that" chain of filters. If the first doesn't succeed, |
| 57 |
// then it tries the other. |
| 58 |
// |
| 59 |
// So, those `math` routes could have been mounted all as one, with `or`. |
| 60 |
// |
| 61 |
// GET /math/sum/:u32/:u32 |
| 62 |
// GET /math/:u16/times/:u16 |
| 63 |
let math = warp::path("math").and(sum.or(times)); |
| 64 |
|
| 65 |
// We can use the end() filter to match a shorter path |
| 66 |
let help = warp::path("math") |
| 67 |
// Careful! Omitting the following line would make this filter match |
| 68 |
// requests to /math/sum/:u32/:u32 and /math/:u16/times/:u16 |
| 69 |
.and(warp::path::end()) |
| 70 |
.map(|| "This is the Math API. Try calling /math/sum/:u32/:u32 or /math/:u16/times/:u16"); |
| 71 |
let math = help.or(math); |
| 72 |
|
| 73 |
// Let's let people know that the `sum` and `times` routes are under `math`. |
| 74 |
let sum = sum.map(|output| format!("(This route has moved to /math/sum/:u16/:u16) {}", output)); |
| 75 |
let times = |
| 76 |
times.map(|output| format!("(This route has moved to /math/:u16/times/:u16) {}", output)); |
| 77 |
|
| 78 |
// It turns out, using `or` is how you combine everything together into |
| 79 |
// a single API. (We also actually haven't been enforcing that the |
| 80 |
// method is GET, so we'll do that too!) |
| 81 |
// |
| 82 |
// GET / |
| 83 |
// GET /hi |
| 84 |
// GET /hello/from/warp |
| 85 |
// GET /bye/:string |
| 86 |
// GET /math/sum/:u32/:u32 |
| 87 |
// GET /math/:u16/times/:u16 |
| 88 |
|
| 89 |
let routes = warp::get().and( |
| 90 |
hello_world |
| 91 |
.or(hi) |
| 92 |
.or(hello_from_warp) |
| 93 |
.or(bye) |
| 94 |
.or(math) |
| 95 |
.or(sum) |
| 96 |
.or(times), |
| 97 |
); |
| 98 |
|
| 99 |
// Note that composing filters for many routes may increase compile times (because it uses a lot of generics). |
| 100 |
// If you wish to use dynamic dispatch instead and speed up compile times while |
| 101 |
// making it slightly slower at runtime, you can use Filter::boxed(). |
| 102 |
|
| 103 |
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await; |
| 104 |
} |