hyperlane_plugin_static_files/struct.rs
1use super::*;
2
3/// Configuration for [`StaticFilesRuntime`] and [`StaticFilesPlugin`].
4#[derive(Clone, Data, Debug, Eq, PartialEq)]
5pub struct StaticFilesConfig {
6 /// Filesystem root directory from which assets are served.
7 pub(crate) root_dir: PathBuf,
8 /// URL mount prefix (for example `"/static"` or `"/"`).
9 pub(crate) mount_prefix: String,
10 /// Default directory index filename (default `"index.html"`).
11 pub(crate) index_file: String,
12 /// Optional SPA fallback file relative to `root_dir` (for example `"index.html"`).
13 #[get(type(clone))]
14 pub(crate) spa_fallback: Option<String>,
15 /// `Cache-Control` header value applied to served files.
16 pub(crate) cache_control: String,
17 /// Enables serving precompressed `.br` and `.gz` sidecar files when accepted by the client.
18 #[get(type(copy))]
19 pub(crate) serve_precompressed: bool,
20 /// Permits serving dotfiles (`.well-known`, etc.) when `true` (default `false`).
21 #[get(type(copy))]
22 pub(crate) allow_dotfiles: bool,
23 /// Total memory budget in bytes for the 64-way sharded hot-file cache (`0` disables caching).
24 #[get(type(copy))]
25 pub(crate) max_cache_bytes: usize,
26 /// Maximum single file size eligible for in-memory hot caching.
27 #[get(type(copy))]
28 pub(crate) max_file_cache_bytes: usize,
29}
30
31/// Immutable cached static file payload stored inside [`StaticFilesRuntime`].
32#[derive(Clone, Debug)]
33pub(crate) struct CachedFile {
34 /// Shared zero-copy file contents bytes.
35 pub(crate) bytes: Arc<[u8]>,
36 /// Precomputed strong `ETag`.
37 pub(crate) etag: String,
38 /// Resolved MIME `Content-Type`.
39 pub(crate) content_type: &'static str,
40 /// Optional precompressed `Content-Encoding` (`"br"` or `"gzip"`).
41 pub(crate) content_encoding: Option<&'static str>,
42}
43
44/// Cached asset entry holding the uncompressed representation plus optional `.br` and `.gz` sidecars.
45#[derive(Clone, Debug)]
46pub(crate) struct CachedRouteEntry {
47 /// Uncompressed file representation.
48 pub(crate) raw: Arc<CachedFile>,
49 /// Optional Brotli precompressed sidecar representation.
50 pub(crate) br: Option<Arc<CachedFile>>,
51 /// Optional Gzip precompressed sidecar representation.
52 pub(crate) gz: Option<Arc<CachedFile>>,
53}
54
55/// Shared interior state of [`StaticFilesRuntime`].
56pub(crate) struct StaticFilesRuntimeInner {
57 /// Original static file configuration.
58 pub(crate) config: StaticFilesConfig,
59 /// Canonicalized root directory path for symlink-escape verification.
60 pub(crate) canonical_root: PathBuf,
61 /// Normalized mount prefix (without trailing slash unless `"/"`).
62 pub(crate) normalized_prefix: String,
63 /// Current cached byte usage across all 64 shards.
64 pub(crate) cached_bytes: AtomicUsize,
65 /// 64-way sharded hot-file memory cache.
66 pub(crate) cache_shards: StaticCacheShardArray,
67}
68
69/// Thread-safe static asset engine with 64-way sharded hot-file caching and security checks.
70#[derive(Clone)]
71pub struct StaticFilesRuntime {
72 /// Shared runtime state.
73 pub(crate) inner: Arc<StaticFilesRuntimeInner>,
74}
75
76/// Process-wide [`StaticFilesPlugin`] singleton storing [`StaticFilesRuntime`].
77#[derive(Clone)]
78pub struct StaticFilesPlugin {
79 /// Compiled static-file serving engine.
80 pub(crate) runtime: StaticFilesRuntime,
81}
82
83/// Opt-in [`ServerHook`] serving static assets on `GET` / `HEAD` requests under the configured mount prefix.
84#[derive(Clone, Copy, Debug, Default)]
85#[hyperlane::request_middleware(-55)]
86pub struct StaticFilesHook;