A simple static file serving component for Rust's Tide web framework.
This code is based heavily on this archived example.
To use the library:
StaticRootDir
on your state. This tells the library how to access the name of the root directory in which your static assets live.get
endpoint with a *path
glob pattern (like /static/*path
or /*path
) and have it call the serve_static_files
function.```rust use std::path::{Path, PathBuf}; use tidenaivestaticfiles::{servestatic_files, StaticRootDir};
struct AppState { // 1. staticrootdir: PathBuf, }
impl StaticRootDir for AppState { // 2. fn rootdir(&self) -> &Path { &self.staticroot_dir } }
async fn main() { let state = AppState { staticrootdir: "./examples/".into(), };
let mut app = tide::with_state(state);
app.at("/static/*path") // 3.
.get(|req| async { serve_static_files(req).await.unwrap() });
app.listen("127.0.0.1:8000").await.unwrap();
} ```
Right now it kinda doesn't use all the async-y-ness that it probably could. There are a couple of unfortunate task::block_on
s that I want to get rid of. Suggestions welcome!