Debian Codegraph @

third_party/rust/warp/examples/compression.rs

raw
1 #![deny(warnings)]
2
3 use warp::Filter;
4
5 #[tokio::main]
6 async fn main() {
7 let file = warp::path("todos").and(warp::fs::file("./examples/todos.rs"));
8 // NOTE: You could double compress something by adding a compression
9 // filter here, a la
10 // ```
11 // let file = warp::path("todos")
12 // .and(warp::fs::file("./examples/todos.rs"))
13 // .with(warp::compression::brotli());
14 // ```
15 // This would result in a browser error, or downloading a file whose contents
16 // are compressed
17
18 let dir = warp::path("ws_chat").and(warp::fs::file("./examples/websockets_chat.rs"));
19
20 let file_and_dir = warp::get()
21 .and(file.or(dir))
22 .with(warp::compression::gzip());
23
24 let examples = warp::path("ex")
25 .and(warp::fs::dir("./examples/"))
26 .with(warp::compression::deflate());
27
28 // GET /todos => gzip -> toods.rs
29 // GET /ws_chat => gzip -> ws_chat.rs
30 // GET /ex/... => deflate -> ./examples/...
31 let routes = file_and_dir.or(examples);
32
33 warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
34 }