Skip to content

Commit 50f2df3

Browse files
committed
Rework fs filters to be more configurable
1 parent 7b07043 commit 50f2df3

File tree

9 files changed

+487
-65
lines changed

9 files changed

+487
-65
lines changed

examples/compression.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@ use warp::Filter;
44

55
#[tokio::main]
66
async fn main() {
7-
let file = warp::path("todos").and(warp::fs::file("./examples/todos.rs"));
7+
let file = warp::path("todos").and(warp::fs::config().file("./examples/todos.rs"));
88
// NOTE: You could double compress something by adding a compression
99
// filter here, a la
1010
// ```
1111
// let file = warp::path("todos")
12-
// .and(warp::fs::file("./examples/todos.rs"))
12+
// .and(warp::fs::config().file("./examples/todos.rs"))
1313
// .with(warp::compression::brotli());
1414
// ```
1515
// This would result in a browser error, or downloading a file whose contents
1616
// are compressed
1717

18-
let dir = warp::path("ws_chat").and(warp::fs::file("./examples/websockets_chat.rs"));
18+
let dir = warp::path("ws_chat").and(warp::fs::config().file("./examples/websockets_chat.rs"));
1919

2020
let file_and_dir = warp::get()
2121
.and(file.or(dir))
2222
.with(warp::compression::gzip());
2323

2424
let examples = warp::path("ex")
25-
.and(warp::fs::dir("./examples/"))
25+
.and(warp::fs::config().dir("./examples/"))
2626
.with(warp::compression::deflate());
2727

2828
// GET /todos => gzip -> toods.rs

examples/dir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
async fn main() {
55
pretty_env_logger::init();
66

7-
warp::serve(warp::fs::dir("examples/dir"))
7+
warp::serve(warp::fs::config().dir("examples/dir"))
88
.run(([127, 0, 0, 1], 3030))
99
.await;
1010
}

examples/file.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ async fn main() {
88

99
let readme = warp::get()
1010
.and(warp::path::end())
11-
.and(warp::fs::file("./README.md"));
11+
.and(warp::fs::config().file("./README.md"));
1212

1313
// dir already requires GET...
14-
let examples = warp::path("ex").and(warp::fs::dir("./examples/"));
14+
let examples = warp::path("ex").and(warp::fs::config().dir("./examples/"));
1515

1616
// GET / => README.md
1717
// GET /ex/... => ./examples/..

examples/returning.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use warp::{filters::BoxedFilter, Filter, Rejection, Reply};
55
// Boxing the filters will use dynamic dispatch and speed up compilation while
66
// making it slightly slower at runtime.
77
pub fn assets_filter() -> BoxedFilter<(impl Reply,)> {
8-
warp::path("assets").and(warp::fs::dir("./assets")).boxed()
8+
warp::path("assets")
9+
.and(warp::fs::config().dir("./assets"))
10+
.boxed()
911
}
1012

1113
// Option 2: impl Filter + Clone

examples/unix_socket.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async fn main() {
1010

1111
let listener = UnixListener::bind("/tmp/warp.sock").unwrap();
1212
let incoming = UnixListenerStream::new(listener);
13-
warp::serve(warp::fs::dir("examples/dir"))
13+
warp::serve(warp::fs::config().dir("examples/dir"))
1414
.run_incoming(incoming)
1515
.await;
1616
}

src/filter/boxed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::reject::Rejection;
2222
///
2323
/// pub fn assets_filter() -> BoxedFilter<(impl Reply,)> {
2424
/// warp::path("assets")
25-
/// .and(warp::fs::dir("./assets"))
25+
/// .and(warp::fs::config().dir("./assets"))
2626
/// .boxed()
2727
/// }
2828
/// ```

src/filters/compression.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub struct Compression<F> {
6363
///
6464
/// let route = warp::get()
6565
/// .and(warp::path::end())
66-
/// .and(warp::fs::file("./README.md"))
66+
/// .and(warp::fs::config().file("./README.md"))
6767
/// .with(warp::compression::gzip());
6868
/// ```
6969
#[cfg(feature = "compression-gzip")]
@@ -92,7 +92,7 @@ pub fn gzip() -> Compression<impl Fn(CompressionProps) -> Response + Copy> {
9292
///
9393
/// let route = warp::get()
9494
/// .and(warp::path::end())
95-
/// .and(warp::fs::file("./README.md"))
95+
/// .and(warp::fs::config().file("./README.md"))
9696
/// .with(warp::compression::deflate());
9797
/// ```
9898
#[cfg(feature = "compression-gzip")]
@@ -121,7 +121,7 @@ pub fn deflate() -> Compression<impl Fn(CompressionProps) -> Response + Copy> {
121121
///
122122
/// let route = warp::get()
123123
/// .and(warp::path::end())
124-
/// .and(warp::fs::file("./README.md"))
124+
/// .and(warp::fs::config().file("./README.md"))
125125
/// .with(warp::compression::brotli());
126126
/// ```
127127
#[cfg(feature = "compression-brotli")]

0 commit comments

Comments
 (0)