rocket_include_static_resources/
lib.rs

1/*!
2# Include Static Resources for Rocket Framework
3
4This 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.
5
6## Example
7
8```rust,ignore
9#[macro_use]
10extern crate rocket;
11
12#[macro_use]
13extern crate rocket_include_static_resources;
14
15use rocket::State;
16
17use rocket_include_static_resources::{EtagIfNoneMatch, StaticContextManager, StaticResponse};
18
19static_response_handler! {
20    "/favicon.ico" => favicon => "favicon",
21    "/favicon-16.png" => favicon_png => "favicon-png",
22}
23
24#[get("/")]
25fn index(
26    static_resources: &State<StaticContextManager>,
27    etag_if_none_match: EtagIfNoneMatch,
28) -> StaticResponse {
29    static_resources.build(&etag_if_none_match, "html-readme")
30}
31
32#[launch]
33fn rocket() -> _ {
34    rocket::build()
35        .attach(static_resources_initializer!(
36            "favicon" => "examples/front-end/images/favicon.ico",
37            "favicon-png" => "examples/front-end/images/favicon-16.png",
38            "html-readme" => ("examples", "front-end", "html", "README.html"),
39        ))
40        .mount("/", routes![favicon, favicon_png])
41        .mount("/", routes![index])
42}
43```
44
45* `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.
46* `static_response_handler!` is used for quickly creating **GET** route handlers to retrieve static resources.
47
48See `examples`.
49*/
50
51#[doc(hidden)]
52pub extern crate rocket;
53
54#[doc(hidden)]
55pub extern crate mime;
56
57#[doc(hidden)]
58pub extern crate manifest_dir_macros;
59
60mod functions;
61
62mod macros;
63
64#[cfg(debug_assertions)]
65mod debug;
66
67#[cfg(not(debug_assertions))]
68mod release;
69
70#[cfg(debug_assertions)]
71pub use debug::*;
72#[cfg(not(debug_assertions))]
73pub use release::*;
74#[cfg(feature = "cache")]
75pub use rocket_cache_response::CacheResponse;
76pub use rocket_etag_if_none_match::{entity_tag::EntityTag, EtagIfNoneMatch};