Crate trillium_static_compiled[−][src]
Expand description
Serves static file assets from the file system.
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.
This may also merge with the static file handler
use trillium_static_compiled::{include_dir, StaticCompiledHandler};
let handler = StaticCompiledHandler::new(include_dir!("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 = StaticCompiledHandler::new(include_dir!("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 = StaticCompiledHandler::new(include_dir!("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));
Macros
include a directory relative to the current crate root. this will panic if the directory cannot be found or accessed.
The preferred interface to build a StaticCompiledHandler
macro to attempt to include a directory relative to the current crate root. if it cannot find the directory, it will return a static Err result with a &’static str. this will never panic.
Structs
A directory entry.
A file with its contents stored in a &'static [u8]
.
The static compiled handler which contains the compile-time loaded assets
Functions
Alias for StaticCompiledHandler::new