Expand description
Easily serve static assets with configurable cache policy from Rocket.
This create adds a fairing and responder for serving static assets. You should configure an
assets directory, attach the fairing, and then return an Asset
on whatever route you want.
§Usage
- Add your assets to the configurable
assets_dir
directory (default:{rocket_root}/assets
). - Optionally configure the cache policy using
assets_max_age
- Attach
Assets::fairing()
and return anAsset
usingAssets::open()
(specifying the relative file path):
use assets_rocket_fairing::{Asset, Assets};
#[rocket::main]
async fn main() {
rocket::build()
.attach(Assets::fairing())
.mount("/assets", routes![style])
.launch()
.await;
}
#[get("/style.css")]
async fn style(assets: &Assets) -> Option<Asset> {
assets.open("style.css").await.ok()
}
Structs§
- An asset that can be returned from a route
- The asset collection located in the configured folder