[−][src]Module vial::asset
Vial can serve static files out of an asset directory and
optionally bundle them into your application in --release
mode.
By setting an asset directory, either through the
[vial::asset_dir!()
][macro.asset_dir.html] or
[vial::bundle_assets!()
][macro.bundle_assets.html] macro,
you can then use the methods in this module to work with them:
- asset::etag(): Get the ETag for an asset. Used automatically by the Router if a web request matches an asset's path.
- asset::exists(): Does an asset exist? Works regardless of whether the asset is bundled or not.
- asset::is_bundled(): Are assets
bundled? Only true in
--release
mode and when used with thevial::bundle_assets!()
macro. - asset::to_string(): Like
fs::read_to_string()
, delivers the content of an asset as aString
. - asset::as_reader(): Like
asset::to_string()
but provides anio::Read
of an asset, whether or not it's bundled.
To get started, put all your .js
and .css
and other static
assets into a directory in the root of your project, then
reference them in HTML as if the root of your Vial web
application was that asset directory.
Next call [vial::asset_dir!()
][macro.asset_dir.html] with the
path to your asset directory (maybe assets/
?) before starting
your application with vial::run!
:
If we had a directory structure like this: . ├── README.md ├── assets │ └── img │ ├── banker.png │ └── doctor.png └── src └── main.rs
We could serve our images like so:
vial::routes! { GET "/" => |_| " <p><img src='/img/doctor.png'/></p> <p><img src='/img/banker.png'/></p> "; } fn main() { vial::asset_dir!("assets/"); vial::run!().unwrap(); }
Functions
as_reader | Produces a boxed |
etag | Produce an etag for an asset. |
exists | Does the asset exist on disk? |
is_bundled | Have assets been bundled into the binary? |
normalize_path | Cleans a path of tricky things like |
read | Read an asset to u8. |
size | Size of an asset in |
to_string | Like fs::read_to_string(), but with an asset. |