Skip to main content

dioxus_server/isrg/
config.rs

1#![allow(non_snake_case)]
2
3#[cfg(not(target_arch = "wasm32"))]
4use crate::isrg::fs_cache::PathMapFn;
5
6use crate::IncrementalRenderer;
7use crate::isrg::memory_cache::InMemoryCache;
8
9use std::{
10    path::{Path, PathBuf},
11    time::Duration,
12};
13
14/// A configuration for the incremental renderer.
15#[derive(Clone)]
16pub struct IncrementalRendererConfig {
17    static_dir: PathBuf,
18    memory_cache_limit: usize,
19    invalidate_after: Option<Duration>,
20    clear_cache: bool,
21
22    #[cfg(not(target_arch = "wasm32"))]
23    map_path: Option<PathMapFn>,
24}
25
26impl Default for IncrementalRendererConfig {
27    fn default() -> Self {
28        Self::new()
29    }
30}
31
32impl IncrementalRendererConfig {
33    /// Create a new incremental renderer configuration.
34    pub fn new() -> Self {
35        Self {
36            static_dir: PathBuf::from("./static"),
37            memory_cache_limit: 10000,
38            invalidate_after: None,
39            clear_cache: false,
40            #[cfg(not(target_arch = "wasm32"))]
41            map_path: None,
42        }
43    }
44
45    /// Clear the cache on startup (default: true)
46    pub fn clear_cache(mut self, clear_cache: bool) -> Self {
47        self.clear_cache = clear_cache;
48        self
49    }
50
51    /// Set a mapping from the route to the file path. This will override the default mapping configured with `static_dir`.
52    /// The function should return the path to the folder to store the index.html file in.
53    #[cfg(not(target_arch = "wasm32"))]
54    pub fn map_path<F: Fn(&str) -> PathBuf + Send + Sync + 'static>(mut self, map_path: F) -> Self {
55        self.map_path = Some(std::sync::Arc::new(map_path));
56        self
57    }
58
59    /// Set the static directory.
60    pub fn static_dir<P: AsRef<Path>>(mut self, static_dir: P) -> Self {
61        self.static_dir = static_dir.as_ref().to_path_buf();
62        self
63    }
64
65    /// Set the memory cache limit.
66    pub const fn memory_cache_limit(mut self, memory_cache_limit: usize) -> Self {
67        self.memory_cache_limit = memory_cache_limit;
68        self
69    }
70
71    /// Set the invalidation time.
72    pub fn invalidate_after(mut self, invalidate_after: Duration) -> Self {
73        self.invalidate_after = Some(invalidate_after);
74        self
75    }
76
77    /// Build the incremental renderer.
78    pub fn build(self) -> IncrementalRenderer {
79        let mut renderer = IncrementalRenderer {
80            #[cfg(not(target_arch = "wasm32"))]
81            file_system_cache: super::fs_cache::FileSystemCache::new(
82                self.static_dir.clone(),
83                self.map_path,
84                self.invalidate_after,
85            ),
86            memory_cache: InMemoryCache::new(self.memory_cache_limit, self.invalidate_after),
87            invalidate_after: self.invalidate_after,
88        };
89
90        if self.clear_cache {
91            renderer.invalidate_all();
92        }
93
94        renderer
95    }
96}