A simple static file serving component for Rust's Tide web framework.
This code is based heavily on this archived example.
This crate is not officially associated with the tide
project, it's more of an interim solution while tide
is still in a state of (relative) flux.
Mistakes were made when initially selecting version numbers for this crate. In the Rust ecosystem, a 1.0.0 release generally means the crate is fit for production. This crate makes no such claim. It would be best to "divide by ten" when looking at the crate's version number (i.e. 2.0.1 should be thought of as 0.2.0.1).
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::{servestaticfiles, StaticRootDir}; use asyncstd::task;
struct AppState { // 1. staticrootdir: PathBuf, }
impl StaticRootDir for AppState { // 2. fn rootdir(&self) -> &Path { &self.staticroot_dir } }
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() });
task::block_on(async move {
app.listen("127.0.0.1:8000").await.unwrap();
})
} ```
task::block_on
sRight 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!