6 个版本

0.4.2 2024 年 4 月 7 日
0.4.1 2024 年 1 月 2 日
0.4.0 2021 年 10 月 10 日
0.3.1 2021 年 9 月 9 日
0.1.0 2021 年 6 月 21 日

#23 in #trillium

每月 37 次下载
用于 trillium-cli

MIT/Apache

76KB
1K SLoC

欢迎使用 Trillium!

📖 指南 📖

指南提供了架构概述和连接 trillium 库的概述。

📑 Rustdocs 📑

Rustdocs 是了解 trillium 的各个库及其特定接口的最好方式。




法律

许可协议为以下之一

任选其一。

除非你明确说明,否则根据 Apache-2.0 许可证定义的,你有意提交的任何贡献,都应按上述方式双重许可,不附加任何额外条款或条件。


lib.rs:

从文件系统服务静态文件资源。

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"
);

❗重要❗

此 crate 当前有三个功能: smolasync-stdtokio

你必须启用其中一个才能使用此 crate。这将是临时情况,最终你不需要通过功能标志指定运行时。

稳定性说明

请注意,此 crate 功能尚不完整,但仍可运行。它不包含任何关于范围请求或缓存头的信息。每次都从磁盘服务所有文件,没有内存缓存。

依赖项

~6–18MB
~292K SLoC