6 releases (3 breaking)

0.5.1 Jan 2, 2024
0.5.0 Jan 3, 2022
0.4.0 Dec 3, 2021
0.3.1 Sep 9, 2021
0.1.0 Jun 7, 2021

#22 in #trillium

Download history 15/week @ 2023-11-06 49/week @ 2023-11-13 46/week @ 2023-11-20 71/week @ 2023-11-27 39/week @ 2023-12-04 81/week @ 2023-12-11 29/week @ 2023-12-18 36/week @ 2023-12-25 96/week @ 2024-01-01 67/week @ 2024-01-08 46/week @ 2024-01-15 100/week @ 2024-01-22 73/week @ 2024-01-29 43/week @ 2024-02-05 127/week @ 2024-02-12 208/week @ 2024-02-19

451 downloads per month
Used in objstor

MIT/Apache

77KB
1K SLoC

Welcome to Trillium!

📖 Guide 📖

The guide provides an architectural overview and lay of the land connecting the trillium crates.

📑 Rustdocs 📑

The rustdocs represent the best way to learn about any of trillium's individual crates and the specific interfaces.




Legal:

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

Serves static file assets from memory, as included in the binary at compile time. Because this includes file system content at compile time, it requires a macro interface, static_compiled.

If the root is a directory, it will recursively serve any files relative to the path that this handler is mounted at, or an index file if one is configured with with_index_file.

If the root is a file, it will serve that file at all request paths.

This crate contains code from include_dir, but with several tweaks to make it more suitable for this specific use case.

# #[cfg(not(unix))] fn main() {}
# #[cfg(unix)] fn main() {
use trillium_static_compiled::static_compiled;

let handler = static_compiled!("./examples/files")
.with_index_file("index.html");

// given the following directory layout
//
// examples/files
// ├── index.html
// ├── subdir
// │  └── index.html
// └── subdir_with_no_index
//    └── plaintext.txt
//

use trillium_testing::prelude::*;

assert_ok!(
get("/").on(&handler),
"<html>\n  <head>\n    <script src=\"/js.js\"></script>\n  </head>\n  <body>\n    <h1>hello world</h1>\n  </body>\n</html>",
"content-type" => "text/html"
);
assert_not_handled!(get("/file_that_does_not_exist.txt").on(&handler));
assert_ok!(get("/index.html").on(&handler));
assert_ok!(
get("/subdir/index.html").on(&handler),
"subdir index.html 🎈",
"content-type" => "text/html; charset=utf-8"
);
assert_ok!(get("/subdir").on(&handler), "subdir index.html 🎈");
assert_not_handled!(get("/subdir_with_no_index").on(&handler));
assert_ok!(
get("/subdir_with_no_index/plaintext.txt").on(&handler),
"plaintext file",
"content-type" => "text/plain"
);


// with a different index file
let plaintext_index = static_compiled!("./examples/files")
.with_index_file("plaintext.txt");

assert_not_handled!(get("/").on(&plaintext_index));
assert_not_handled!(get("/subdir").on(&plaintext_index));
assert_ok!(
get("/subdir_with_no_index").on(&plaintext_index),
"plaintext file",
"content-type" => "text/plain"
);

// with no index file
let no_index = static_compiled!("./examples/files");

assert_not_handled!(get("/").on(&no_index));
assert_not_handled!(get("/subdir").on(&no_index));
assert_not_handled!(get("/subdir_with_no_index").on(&no_index));
# }

Dependencies

~7.5MB
~190K SLoC