Expand description
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.
use trillium_static_compiled::static_compiled;
use trillium_testing::TestServer;
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
let app = TestServer::new(handler).await;
let index = include_str!("../examples/files/index.html");
app.get("/")
.await
.assert_ok()
.assert_body(index)
.assert_header("content-type", "text/html");
app.get("/file_that_does_not_exist.txt")
.await
.assert_status(404);
app.get("/index.html").await.assert_ok();
app.get("/subdir/index.html")
.await
.assert_ok()
.assert_body("subdir index.html 🎈\n")
.assert_header("content-type", "text/html; charset=utf-8");
app.get("/subdir")
.await
.assert_ok()
.assert_body("subdir index.html 🎈\n");
app.get("/subdir_with_no_index").await.assert_status(404);
app.get("/subdir_with_no_index/plaintext.txt")
.await
.assert_ok()
.assert_body("plaintext file\n")
.assert_header("content-type", "text/plain");
// with a different index file
let plaintext_index = static_compiled!("./examples/files").with_index_file("plaintext.txt");
let app2 = TestServer::new(plaintext_index).await;
app2.get("/").await.assert_status(404);
app2.get("/subdir").await.assert_status(404);
app2.get("/subdir_with_no_index")
.await
.assert_ok()
.assert_body("plaintext file\n")
.assert_header("content-type", "text/plain");
// with no index file
let no_index = static_compiled!("./examples/files");
let app3 = TestServer::new(no_index).await;
app3.get("/").await.assert_status(404);
app3.get("/subdir").await.assert_status(404);
app3.get("/subdir_with_no_index").await.assert_status(404);Macros§
- root
- Include the path as root. To be passed into
StaticCompiledHandler::new. - static_
compiled - The preferred interface to build a StaticCompiledHandler
Structs§
- Static
Compiled Handler - The static compiled handler which contains the compile-time loaded assets