#http #rocket #static #web #server

macro rust-embed

Rust Custom Derive Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev

16 releases (7 stable)

3.0.2 Sep 5, 2018
3.0.1 Jul 24, 2018
3.0.0 Jun 2, 2018
2.0.0 May 26, 2018
0.2.0 Mar 16, 2017

#17 in HTTP server

Download history 125/week @ 2018-06-13 45/week @ 2018-06-20 131/week @ 2018-06-27 144/week @ 2018-07-04 132/week @ 2018-07-11 86/week @ 2018-07-18 38/week @ 2018-07-25 86/week @ 2018-08-01 129/week @ 2018-08-08 87/week @ 2018-08-15 29/week @ 2018-08-22 53/week @ 2018-08-29 83/week @ 2018-09-05

389 downloads per month
Used in 3 crates

MIT license

1MB
125 lines

Rust Embed Build Status Build status crates.io

Rust Custom Derive Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.

You can use this to embed your css, js and images into a single executable which can be deployed to your servers. Also it makes it easy to build a very small docker image for you to deploy.

Dev

Release

Installation

[dependencies]
rust-embed="4.0.0"

Documentation

You need to add the custom derive macro RustEmbed to your struct with an attribute folder which is the path to your static folder.

#[derive(RustEmbed)]
#[folder = "examples/public/"]
struct Asset;

This macro adds a single static method get to your type. This method allows you to get your assets from the fs during dev and from the binary during release. It takes the file path as string and returns an Option with the bytes.

pub fn get(file_path: &str) -> Option<impl AsRef<[u8]>>

Usage

#[macro_use]
extern crate rust_embed;

#[derive(RustEmbed)]
#[folder = "examples/public/"]
struct Asset;

fn main() {
  let index_html = Asset::get("index.html").unwrap();
  println!("{:?}", std::str::from_utf8(index_html.as_ref()));
}

Examples

To run the example in dev mode where it reads from the fs,

cargo run --example basic

To run the example in release mode where it reads from binary,

cargo run --example basic --release

Note: To run the actix-web example:

cargo run --example actix --features actix

Note: To run the rocket example, add the nightly feature flag and run on a nightly build:

cargo +nightly run --example rocket --features nightly

Testing

debug: cargo test --test lib

release: cargo test --test lib --release

Go Rusketeers! The power is yours!

Dependencies

~5.5MB
~128K SLoC