[][src]Crate rocket_include_static_resources

Include Static Resources for Rocket Framework

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

This example is not tested
#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use]
extern crate rocket;

#[macro_use]
extern crate rocket_include_static_resources;

use rocket_include_static_resources::{EtagIfNoneMatch, StaticResponse};

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

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

#[get("/")]
fn index(etag_if_none_match: EtagIfNoneMatch) -> StaticResponse {
    static_response!(etag_if_none_match, "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 for including files into your executable binary file. You need to specify each file's name and its path. For instance, the above example uses favicon to represent the file included-static-resources/favicon.ico and favicon_png to represent the file included-static-resources/favicon.png. A name cannot be repeating. 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.

Macros

static_resources_initialize

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.

static_response

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.

Structs

EntityTag

An entity tag, defined in RFC7232

EtagIfNoneMatch

The request guard used for getting if-none-match header.

FileResources

Reloadable file resources.

StaticContextManager

To monitor the state of static resources.

StaticResources

Static resources.

StaticResponse

To respond a static resource.