summaryrefslogtreecommitdiffstats
path: root/third_party/rust/warp/examples/routing.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:47:29 +0000
commit0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch)
treea31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /third_party/rust/warp/examples/routing.rs
parentInitial commit. (diff)
downloadfirefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz
firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/warp/examples/routing.rs')
-rw-r--r--third_party/rust/warp/examples/routing.rs104
1 files changed, 104 insertions, 0 deletions
diff --git a/third_party/rust/warp/examples/routing.rs b/third_party/rust/warp/examples/routing.rs
new file mode 100644
index 0000000000..b2ad8c278d
--- /dev/null
+++ b/third_party/rust/warp/examples/routing.rs
@@ -0,0 +1,104 @@
+#![deny(warnings)]
+
+use warp::Filter;
+
+#[tokio::main]
+async fn main() {
+ pretty_env_logger::init();
+
+ // We'll start simple, and gradually show how you combine these powers
+ // into super powers!
+
+ // GET /
+ let hello_world = warp::path::end().map(|| "Hello, World at root!");
+
+ // GET /hi
+ let hi = warp::path("hi").map(|| "Hello, World!");
+
+ // How about multiple segments? First, we could use the `path!` macro:
+ //
+ // GET /hello/from/warp
+ let hello_from_warp = warp::path!("hello" / "from" / "warp").map(|| "Hello from warp!");
+
+ // Fine, but how do I handle parameters in paths?
+ //
+ // GET /sum/:u32/:u32
+ let sum = warp::path!("sum" / u32 / u32).map(|a, b| format!("{} + {} = {}", a, b, a + b));
+
+ // Any type that implements FromStr can be used, and in any order:
+ //
+ // GET /:u16/times/:u16
+ let times =
+ warp::path!(u16 / "times" / u16).map(|a, b| format!("{} times {} = {}", a, b, a * b));
+
+ // Oh shoot, those math routes should be mounted at a different path,
+ // is that possible? Yep.
+ //
+ // GET /math/sum/:u32/:u32
+ // GET /math/:u16/times/:u16
+ let math = warp::path("math");
+ let _sum = math.and(sum);
+ let _times = math.and(times);
+
+ // What! And? What's that do?
+ //
+ // It combines the filters in a sort of "this and then that" order. In
+ // fact, it's exactly what the `path!` macro has been doing internally.
+ //
+ // GET /bye/:string
+ let bye = warp::path("bye")
+ .and(warp::path::param())
+ .map(|name: String| format!("Good bye, {}!", name));
+
+ // Ah, can filters do things besides `and`?
+ //
+ // Why, yes they can! They can also `or`! As you might expect, `or` creates
+ // a "this or else that" chain of filters. If the first doesn't succeed,
+ // then it tries the other.
+ //
+ // So, those `math` routes could have been mounted all as one, with `or`.
+ //
+ // GET /math/sum/:u32/:u32
+ // GET /math/:u16/times/:u16
+ let math = warp::path("math").and(sum.or(times));
+
+ // We can use the end() filter to match a shorter path
+ let help = warp::path("math")
+ // Careful! Omitting the following line would make this filter match
+ // requests to /math/sum/:u32/:u32 and /math/:u16/times/:u16
+ .and(warp::path::end())
+ .map(|| "This is the Math API. Try calling /math/sum/:u32/:u32 or /math/:u16/times/:u16");
+ let math = help.or(math);
+
+ // Let's let people know that the `sum` and `times` routes are under `math`.
+ let sum = sum.map(|output| format!("(This route has moved to /math/sum/:u16/:u16) {}", output));
+ let times =
+ times.map(|output| format!("(This route has moved to /math/:u16/times/:u16) {}", output));
+
+ // It turns out, using `or` is how you combine everything together into
+ // a single API. (We also actually haven't been enforcing that the
+ // method is GET, so we'll do that too!)
+ //
+ // GET /
+ // GET /hi
+ // GET /hello/from/warp
+ // GET /bye/:string
+ // GET /math/sum/:u32/:u32
+ // GET /math/:u16/times/:u16
+
+ let routes = warp::get().and(
+ hello_world
+ .or(hi)
+ .or(hello_from_warp)
+ .or(bye)
+ .or(math)
+ .or(sum)
+ .or(times),
+ );
+
+ // Note that composing filters for many routes may increase compile times (because it uses a lot of generics).
+ // If you wish to use dynamic dispatch instead and speed up compile times while
+ // making it slightly slower at runtime, you can use Filter::boxed().
+
+ warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
+}