#rocket #server #web #static #file

rocket-include-static-resources

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

41 releases

Uses new Rust 2021

0.10.3 Nov 3, 2022
0.10.2 May 10, 2022
0.10.1 Mar 19, 2022
0.10.0 Aug 13, 2021
0.6.4 Nov 18, 2018

#175 in Web programming

Download history 104/week @ 2022-08-16 155/week @ 2022-08-23 105/week @ 2022-08-30 117/week @ 2022-09-06 129/week @ 2022-09-13 60/week @ 2022-09-20 113/week @ 2022-09-27 177/week @ 2022-10-04 92/week @ 2022-10-11 56/week @ 2022-10-18 125/week @ 2022-10-25 204/week @ 2022-11-01 131/week @ 2022-11-08 140/week @ 2022-11-15 80/week @ 2022-11-22 57/week @ 2022-11-29

429 downloads per month
Used in 4 crates

MIT license

25KB
531 lines

Include Static Resources for Rocket Framework

CI

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

Example

#[macro_use]
extern crate rocket;

#[macro_use]
extern crate rocket_include_static_resources;

use rocket::State;

use rocket_include_static_resources::{EtagIfNoneMatch, StaticContextManager, StaticResponse};

static_response_handler! {
    "/favicon.ico" => favicon => "favicon",
    "/favicon-16.png" => favicon_png => "favicon-png",
}

#[get("/")]
fn index(
    static_resources: &State<StaticContextManager>,
    etag_if_none_match: EtagIfNoneMatch,
) -> StaticResponse {
    static_resources.build(&etag_if_none_match, "html-readme")
}

#[launch]
fn rocket() -> _ {
    rocket::build()
        .attach(static_resources_initializer!(
            "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])
}
  • static_resources_initializer! is used for including files into your executable binary file. You need to specify each file's name and its path relative to the directory containing the manifest of your package. 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_handler! is used for quickly creating GET route handlers to retrieve static resources.

See examples.

Crates.io

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

Documentation

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

License

MIT

Dependencies

~13–46MB
~840K SLoC