6 releases

0.4.2 Apr 7, 2024
0.4.1 Jan 2, 2024
0.4.0 Oct 10, 2021
0.3.1 Sep 9, 2021
0.1.0 Jun 21, 2021

#19 in #trillium


Used in trillium-cli

MIT/Apache

76KB
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 the file system.

use trillium_static::{StaticFileHandler, crate_relative_path};


let mut handler = StaticFileHandler::new(crate_relative_path!("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::*;

handler.init(&mut "testing".into()).await;

assert_ok!(
get("/").run_async(&handler).await,
"<h1>hello world</h1>",
"content-type" => "text/html; charset=utf-8"
);
assert_not_handled!(get("/file_that_does_not_exist.txt").run_async(&handler).await);
assert_ok!(get("/index.html").run_async(&handler).await);
assert_ok!(get("/subdir/index.html").run_async(&handler).await, "subdir index.html");
assert_ok!(get("/subdir").run_async(&handler).await, "subdir index.html");
assert_not_handled!(get("/subdir_with_no_index").run_async(&handler).await);
assert_ok!(
get("/subdir_with_no_index/plaintext.txt").run_async(&handler).await,
"plaintext file",
"content-type" => "text/plain; charset=utf-8"
);


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

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

❗IMPORTANT❗

this crate has three features currently: smol, async-std, and tokio.

You must enable one of these in order to use the crate. This is intended to be a temporary situation, and eventually you will not need to specify the runtime through feature flags.

stability note

Please note that this crate is fairly incomplete, while functional. It does not include any notion of range requests or cache headers. It serves all files from disk every time, with no in-memory caching.

Dependencies

~6–18MB
~302K SLoC