All Projects → pyros2097 → Rust Embed

pyros2097 / Rust Embed

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

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to Rust Embed

Glass Isc Dhcp
Glass - ISC DHCP Server Interface
Stars: ✭ 486 (-11.31%)
Mutual labels:  server
Knowledge Kit
iOS、Web前端、后端、数据库、计算机网络、设计模式经验总结
Stars: ✭ 507 (-7.48%)
Mutual labels:  server
Chinachu
Most Lovely DVR Software in Japan.
Stars: ✭ 542 (-1.09%)
Mutual labels:  server
James Project
Emails at the heart of your business logic!
Stars: ✭ 485 (-11.5%)
Mutual labels:  server
Koop
🔮 Transform, query, and download geospatial data on the web.
Stars: ✭ 505 (-7.85%)
Mutual labels:  server
Generator
A code generator for MyBatis.
Stars: ✭ 4,847 (+784.49%)
Mutual labels:  code-generator
Wsgidav
A generic and extendable WebDAV server based on WSGI
Stars: ✭ 476 (-13.14%)
Mutual labels:  server
Live Torrent
Torrent Web Client
Stars: ✭ 546 (-0.36%)
Mutual labels:  server
Nginx Rtmp Docker
Docker image with Nginx using the nginx-rtmp-module module for live multimedia (video) streaming.
Stars: ✭ 506 (-7.66%)
Mutual labels:  server
Ejabberd
Robust, Ubiquitous and Massively Scalable Messaging Platform (XMPP, MQTT, SIP Server)
Stars: ✭ 5,077 (+826.46%)
Mutual labels:  server
Sbt Buildinfo
I know this because build.sbt knows this.
Stars: ✭ 486 (-11.31%)
Mutual labels:  code-generator
Ream
A super-fast SSR framework for Vue.js 3 ⚡️
Stars: ✭ 497 (-9.31%)
Mutual labels:  server
Vim For Server
.vimrc, simple configures for server, without plugins.
Stars: ✭ 524 (-4.38%)
Mutual labels:  server
Laravel Code Generator
An intelligent code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages and/or form-requests! It is extremely flexible and customizable to cover many on the use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.
Stars: ✭ 485 (-11.5%)
Mutual labels:  code-generator
Swagger Express Middleware
Swagger 2.0 middlware and mocks for Express.js
Stars: ✭ 543 (-0.91%)
Mutual labels:  server
Celerio Angular Quickstart
Generate an Angular 5 CRUD application from an existing database schema (we provide a sample one)
Stars: ✭ 483 (-11.86%)
Mutual labels:  code-generator
Abc
A better Deno framework to create web application.
Stars: ✭ 514 (-6.2%)
Mutual labels:  server
Chatengine
Open source mtproto server written in golang with compatible telegram client
Stars: ✭ 544 (-0.73%)
Mutual labels:  server
React View
React View is an interactive playground, documentation and code generator for your components.
Stars: ✭ 544 (-0.73%)
Mutual labels:  code-generator
Nve
Run any command on specific Node.js versions
Stars: ✭ 531 (-3.1%)
Mutual labels:  server

Rust Embed 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="5.9.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.

The path resolution works as follows:

  • In debug and when debug-embed feature is not enabled, the folder path is resolved relative to where the binary is run from.
  • In release or when debug-embed feature is enabled, the folder path is resolved relative to where Cargo.toml is.
#[derive(RustEmbed)]
#[folder = "examples/public/"]
struct Asset;

The macro will generate the following code:

impl Asset {
  pub fn get(file_path: &str) -> Option<Cow<'static, [u8]>> {
    ...
  }

  pub fn iter() -> impl Iterator<Item = Cow<'static, str>> {
    ...
  }
}
impl RustEmbed for Asset {
  fn get(file_path: &str) -> Option<Cow<'static, [u8]>> {
    ...
  }
  fn iter() -> impl Iterator<Item = Cow<'static, str>> {
    ...
  }
}

get(file_path: &str)

Given a relative path from the assets folder returns the bytes if found.

If the feature debug-embed is enabled or the binary compiled in release mode the bytes have been embeded in the binary and a Cow::Borrowed(&'static [u8]) is returned.

Otherwise the bytes are read from the file system on each call and a Cow::Owned(Vec<u8>) is returned.

iter()

Iterates the files in this assets folder.

If the feature debug-embed is enabled or the binary compiled in release mode a static array to the list of relative paths to the files is returned.

Otherwise the files are listed from the file system on each call.

The prefix attribute

You 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, and will be included in the file paths returned by iter.

Features

debug-embed

Always embed the files in the binary, even in debug mode.

interpolate-folder-path

Allow environment variables to be used in the folder path. Example:

#[derive(RustEmbed)]
#[folder = "$CARGO_MANIFEST_DIR/foo"]
struct Asset;

This will pull the foo directory relative to your Cargo.toml file.

compression

Compress each file when embedding into the binary. Compression is done via include-flate.

Usage

use rust_embed::RustEmbed;

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

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

  for file in Asset::iter() {
      println!("{}", file.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

Note: To run the warp example:

cargo run --example warp --features warp-ex

Testing

debug: cargo test --test lib

release: cargo test --test lib --release

Go Rusketeers! The power is yours!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].