Skip to content

Commit

Permalink
Rework fs filters to be more configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
urkle committed Feb 8, 2024
1 parent 7b07043 commit 0ceb619
Show file tree
Hide file tree
Showing 9 changed files with 487 additions and 65 deletions.
8 changes: 4 additions & 4 deletions examples/compression.rs
Expand Up @@ -4,25 +4,25 @@ use warp::Filter;

#[tokio::main]
async fn main() {
let file = warp::path("todos").and(warp::fs::file("./examples/todos.rs"));
let file = warp::path("todos").and(warp::fs::config().file("./examples/todos.rs"));
// NOTE: You could double compress something by adding a compression
// filter here, a la
// ```
// let file = warp::path("todos")
// .and(warp::fs::file("./examples/todos.rs"))
// .and(warp::fs::config().file("./examples/todos.rs"))
// .with(warp::compression::brotli());
// ```
// This would result in a browser error, or downloading a file whose contents
// are compressed

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

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

let examples = warp::path("ex")
.and(warp::fs::dir("./examples/"))
.and(warp::fs::config().dir("./examples/"))
.with(warp::compression::deflate());

// GET /todos => gzip -> toods.rs
Expand Down
2 changes: 1 addition & 1 deletion examples/dir.rs
Expand Up @@ -4,7 +4,7 @@
async fn main() {
pretty_env_logger::init();

warp::serve(warp::fs::dir("examples/dir"))
warp::serve(warp::fs::config().dir("examples/dir"))
.run(([127, 0, 0, 1], 3030))
.await;
}
4 changes: 2 additions & 2 deletions examples/file.rs
Expand Up @@ -8,10 +8,10 @@ async fn main() {

let readme = warp::get()
.and(warp::path::end())
.and(warp::fs::file("./README.md"));
.and(warp::fs::config().file("./README.md"));

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

// GET / => README.md
// GET /ex/... => ./examples/..
Expand Down
4 changes: 3 additions & 1 deletion examples/returning.rs
Expand Up @@ -5,7 +5,9 @@ use warp::{filters::BoxedFilter, Filter, Rejection, Reply};
// Boxing the filters will use dynamic dispatch and speed up compilation while
// making it slightly slower at runtime.
pub fn assets_filter() -> BoxedFilter<(impl Reply,)> {
warp::path("assets").and(warp::fs::dir("./assets")).boxed()
warp::path("assets")
.and(warp::fs::config().dir("./assets"))
.boxed()
}

// Option 2: impl Filter + Clone
Expand Down
2 changes: 1 addition & 1 deletion examples/unix_socket.rs
Expand Up @@ -10,7 +10,7 @@ async fn main() {

let listener = UnixListener::bind("/tmp/warp.sock").unwrap();
let incoming = UnixListenerStream::new(listener);
warp::serve(warp::fs::dir("examples/dir"))
warp::serve(warp::fs::config().dir("examples/dir"))
.run_incoming(incoming)
.await;
}
Expand Down
2 changes: 1 addition & 1 deletion src/filter/boxed.rs
Expand Up @@ -22,7 +22,7 @@ use crate::reject::Rejection;
///
/// pub fn assets_filter() -> BoxedFilter<(impl Reply,)> {
/// warp::path("assets")
/// .and(warp::fs::dir("./assets"))
/// .and(warp::fs::config().dir("./assets"))
/// .boxed()
/// }
/// ```
Expand Down
6 changes: 3 additions & 3 deletions src/filters/compression.rs
Expand Up @@ -63,7 +63,7 @@ pub struct Compression<F> {
///
/// let route = warp::get()
/// .and(warp::path::end())
/// .and(warp::fs::file("./README.md"))
/// .and(warp::fs::config().ile("./README.md"))
/// .with(warp::compression::gzip());
/// ```
#[cfg(feature = "compression-gzip")]
Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn gzip() -> Compression<impl Fn(CompressionProps) -> Response + Copy> {
///
/// let route = warp::get()
/// .and(warp::path::end())
/// .and(warp::fs::file("./README.md"))
/// .and(warp::fs::config().file("./README.md"))
/// .with(warp::compression::deflate());
/// ```
#[cfg(feature = "compression-gzip")]
Expand Down Expand Up @@ -121,7 +121,7 @@ pub fn deflate() -> Compression<impl Fn(CompressionProps) -> Response + Copy> {
///
/// let route = warp::get()
/// .and(warp::path::end())
/// .and(warp::fs::file("./README.md"))
/// .and(warp::fs::config().file("./README.md"))
/// .with(warp::compression::brotli());
/// ```
#[cfg(feature = "compression-brotli")]
Expand Down

0 comments on commit 0ceb619

Please sign in to comment.