Expand description
Creates a Filter
that serves a directory at the base path
joined
by the request path.
This can be used to serve “static files” from a directory. By far the most
common pattern of serving static files is for GET
requests, so this
filter automatically includes a GET
check.
Example
use warp::Filter;
// Matches requests that start with `/static`,
// and then uses the rest of that path to lookup
// and serve a file from `/www/static`.
let route = warp::path("static")
.and(warp::fs::dir("/www/static"));
// For example:
// - `GET /static/app.js` would serve the file `/www/static/app.js`
// - `GET /static/css/app.css` would serve the file `/www/static/css/app.css`
Examples found in repository?
More examples
examples/file.rs (line 14)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
async fn main() {
pretty_env_logger::init();
let readme = warp::get()
.and(warp::path::end())
.and(warp::fs::file("./README.md"));
// dir already requires GET...
let examples = warp::path("ex").and(warp::fs::dir("./examples/"));
// GET / => README.md
// GET /ex/... => ./examples/..
let routes = readme.or(examples);
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}
examples/compression.rs (line 25)
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
async fn main() {
let file = warp::path("todos").and(warp::fs::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"))
// .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 file_and_dir = warp::get()
.and(file.or(dir))
.with(warp::compression::gzip());
let examples = warp::path("ex")
.and(warp::fs::dir("./examples/"))
.with(warp::compression::deflate());
// GET /todos => gzip -> toods.rs
// GET /ws_chat => gzip -> ws_chat.rs
// GET /ex/... => deflate -> ./examples/...
let routes = file_and_dir.or(examples);
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}