#rocket #server #web #static #file

nightly rocket-include-static-resources

This is a crate which provides macros static_resources_initialize! and static_response! to statically include files from your Rust project and make them be the HTTP response sources quickly

37 releases

0.9.6 Mar 11, 2021
0.9.5 Jul 29, 2020
0.9.4 Jun 27, 2020
0.9.3 Sep 19, 2019
0.6.4 Nov 18, 2018

#110 in Web programming

Download history 81/week @ 2021-04-10 175/week @ 2021-04-17 60/week @ 2021-04-24 79/week @ 2021-05-01 68/week @ 2021-05-08 90/week @ 2021-05-15 55/week @ 2021-05-22 75/week @ 2021-05-29 102/week @ 2021-06-05 88/week @ 2021-06-12 57/week @ 2021-06-19 48/week @ 2021-06-26 77/week @ 2021-07-03 95/week @ 2021-07-10 136/week @ 2021-07-17 90/week @ 2021-07-24

379 downloads per month
Used in 4 crates

MIT license

21KB
407 lines

Include Static Resources for Rocket Framework

Build Status

This is a crate which provides macros static_resources_initialize! and static_response! to statically include files from your Rust project and make them be the HTTP response sources quickly.

Example

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use]
extern crate rocket;

#[macro_use]
extern crate rocket_include_static_resources;

use rocket_include_static_resources::StaticResponse;

#[get("/favicon.ico")]
fn favicon() -> StaticResponse {
    static_response!("favicon")
}

#[get("/favicon-16.png")]
fn favicon_png() -> StaticResponse {
    static_response!("favicon-png")
}

#[get("/")]
fn index() -> StaticResponse {
    static_response!("html-readme")
}

fn main() {
    rocket::ignite()
        .attach(StaticResponse::fairing(|resources| {
            static_resources_initialize!(
                resources,

                "favicon", "examples/front-end/images/favicon.ico",
                "favicon-png", "examples/front-end/images/favicon-16.png",

                "html-readme", "examples/front-end/html/README.html",
            );
        }))
        .mount("/", routes![favicon, favicon_png])
        .mount("/", routes![index])
        .launch();
}
  • static_resources_initialize! is used in the fairing of StaticResponse to include static files into your executable binary file. You need to specify each file's name and its path. In order to reduce the compilation time and allow to hot-reload resources, files are compiled into your executable binary file together, only when you are using the release profile.
  • static_response! is used for retrieving the file you input through the macro static_resources_initialize! as a Response instance into which three HTTP headers, Content-Type, Content-Length and Etag, will be automatically added.

See examples.

Crates.io

https://crates.io/crates/rocket-include-static-resources

Documentation

https://docs.rs/rocket-include-static-resources

License

MIT

Dependencies

~9.5MB
~203K SLoC