1 unstable release
0.1.0 | Jan 8, 2022 |
---|
#1768 in Development tools
7KB
105 lines
Rocket Assets Fairing
rocket-assets-fairing
is a Fairing for Rocket for easily serving static assets from a folder, with a nice cache policy.
Installing
Add to your Cargo.toml
:
rocket-assets-fairing = "0.1"
Usage
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()
}
Configuration
This is configurable the same way as Rocket.
Either through Rocket.toml
:
[default]
assets_dir = "assets"
assets_max_age = 86400
Or using environment variables:
ROCKET_ASSETS_DIR
ROCKET_ASSETS_MAX_AGE
lib.rs
:
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
using [Assets::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()
}
Dependencies
~15–49MB
~797K SLoC