Skip to main content

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    /// File contents bytes.
35    pub(crate) bytes: Vec<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    /// Source file modification timestamp in milliseconds for cache freshness validation.
43    pub(crate) modified_ms: u64,
44}
45
46/// Shared interior state of [`StaticFilesRuntime`].
47pub(crate) struct StaticFilesRuntimeInner {
48    /// Original static file configuration.
49    pub(crate) config: StaticFilesConfig,
50    /// Canonicalized root directory path for symlink-escape verification.
51    pub(crate) canonical_root: PathBuf,
52    /// Normalized mount prefix (without trailing slash unless `"/"`).
53    pub(crate) normalized_prefix: String,
54    /// Current cached byte usage across all 64 shards.
55    pub(crate) cached_bytes: AtomicUsize,
56    /// 64-way sharded hot-file memory cache.
57    pub(crate) cache_shards: StaticCacheShardArray,
58}
59
60/// Thread-safe static asset engine with 64-way sharded hot-file caching and security checks.
61#[derive(Clone)]
62pub struct StaticFilesRuntime {
63    /// Shared runtime state.
64    pub(crate) inner: Arc<StaticFilesRuntimeInner>,
65}
66
67/// Process-wide [`StaticFilesPlugin`] singleton storing [`StaticFilesRuntime`].
68#[derive(Clone)]
69pub struct StaticFilesPlugin {
70    /// Compiled static-file serving engine.
71    pub(crate) runtime: StaticFilesRuntime,
72}
73
74/// Opt-in [`ServerHook`] serving static assets on `GET` / `HEAD` requests under the configured mount prefix.
75#[derive(Clone, Copy, Debug, Default)]
76#[hyperlane::request_middleware(-55)]
77pub struct StaticFilesHook;