Rust Macro which embeds files into your executable. A fork of rust-embed
with a focus on usage in web servers.
rust-embed
This 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]
.
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.&'static
reference. This makes is easy to use
the file data in a server response without creating copies or reference
counting.
These differences can be useful for web servers, with the caveat that it will
increase executable size beyond what the original rust-embed
does. If you are
not building a web server, or the size of the executable is important to you,
you should likely use the original project instead.
toml
[dependencies]
rust-embed-for-web="7.0"
To use this macro, add an empty struct, then add the derive. Then, you specify the folder to use.
```rust
struct Asset;
fn main() { let indexhtml = Asset::get("path/index.html").unwrap(); println!("{:?}", std::str::fromutf8(indexhtml.data.asref())); } ```
The path resolution for the folder
is resolved relative to where Cargo.toml
is.
prefix
attributeYou can add #[prefix = "my_prefix/"]
to the RustEmbed
struct to add a prefix
to all of the file paths. This prefix will be required on get
calls.
gzip
attributeYou can add #[gzip = false]
to the RustEmbed
struct to disable gzip
compression for the files in that embed. Only files where the compression
reduces the file size is compressed already; but you can completely disable gzip
compression if you are concerned with file sizes.
interpolate-folder-path
Allow environment variables to be used in the folder
path. Example:
```rust
struct Asset; ```
This will pull the foo
directory relative to your Cargo.toml
file.
include-exclude
Filter files to be embedded with multiple #[include = "*.txt"]
and #[exclude = "*.jpg"]
attributes.
Matching is done on relative file paths, via [globset
].
exclude
attributes have higher priority than include
attributes.
Example:
```rust
struct Asset; ```