| Crates.io | rust-embed-for-web |
| lib.rs | rust-embed-for-web |
| version | 11.2.1 |
| created_at | 2022-10-09 19:18:56.864349+00 |
| updated_at | 2024-05-17 05:24:17.314446+00 |
| description | Rust Macro which embeds files into your executable. A fork of `rust-embed` with a focus on usage on web servers. |
| homepage | |
| repository | https://github.com/SeriousBug/rust-embed-for-web |
| max_upload_size | |
| id | 684149 |
| size | 963,381 |
Rust Macro which embeds files into your executable. A fork of rust-embed with a focus on usage in web servers.
rust-embedThis crate opts to make some choices that may increase the size of your executable in exchange for better performance at runtime. In particular:
#[gzip = false] and #[br = false]
When disabled, the compressed files won't be included for that embed.ETag and Last-Modified
are computed ahead of time and embedded into the executable. This makes it
possible to use these in a web server without any computation at runtime.base85 instead of hex, which is slightly more
compact. When used as ETag values for files in requests, this slightly
reduces the amount of data that has to be transferred.&'static reference. This
makes is easy to use the file data in a server response without creating
copies or reference counting.
[dependencies]
rust-embed-for-web="11.1.4"
To use this macro, add an empty struct, then add the derive. Then, you specify the folder to use.
use rust_embed_for_web::{EmbedableFile, RustEmbed};
#[derive(RustEmbed)]
#[folder = "examples/public/"]
struct Asset;
fn main() {
let index = Asset::get("index.html").unwrap().data();
let contents = std::str::from_utf8(index.as_ref()).unwrap();
println!("Index file: {}", contents);
}
The path for the folder is resolved relative to where Cargo.toml is.
You can add #[gzip = false] and/or #[br = false] attributes to your embed to
disable gzip and brotli compression for the files in that embed.
rust-embed-for-web will only include compressed files where the compression
actually makes files smaller so files that won't compress well like images or
archives already don't include their compressed versions. However you can
Both of the following features are enabled by default.
interpolate-folder-pathAllow environment variables and ~s to be used in the folder path. Example:
#[derive(RustEmbed)]
#[folder = "~/${PROJECT_NAME}/assets"]
struct Asset;
~ will expand into your home folder, and ${PROJECT_NAME} will expand into
the value of the PROJECT_NAME environment variable.
include-excludeYou can filter which files are embedded by adding one or more #[include = "*.txt"] and #[exclude = "*.jpg"] attributes.
Matching is done on relative file paths --the paths you use for the .get call-- via globset.
Excludes are processed first, then includes are applied to grant exceptions.
β οΈ This is different from the original
rust-embedcrate, so double check your include and exclude attributes to make sure the files are correct.
For example, if you wanted to exclude all .svg files except for one named
logo.svg, you could do:
#[derive(RustEmbed)]
#[exclude = "*.svg"]
#[include = "logo.svg"]
#[folder = "assets/"]
struct Assets;
prefixYou can specify a prefix, which will be added to the path of the files. For example:
#[derive(RustEmbed)]
#[folder = "public/"]
#[prefix = "static/"]
struct Asset;
fn main() {
// Say you had a file named "image.png" in a folder named "public".
// You'll get the asset when requesting it with the prefix.
let correct = Asset::get("static/image.png");
// You'll get None, because you didn't specify the prefix
let wrong = Asset::get("image.png");
}
Hengfei Yang π» |
peroxid π |
Nicolas Guiard π» π |