summary refs log tree commit diff
path: root/src/data/mod.rs
diff options
context:
space:
mode:
authortempest <git@ashen.earth>2025-04-18 00:58:28 -0600
committertempest <git@ashen.earth>2025-04-18 00:58:28 -0600
commit619373a261ad18c51cd09bc61d116f585c8295ec (patch)
tree041a80d7b57cf221497809bd3889bff1042b842c /src/data/mod.rs
parentb856f12cf422b96c37c12df3d7829e4d15ef4453 (diff)
Read correct page HEAD main config
Diffstat (limited to 'src/data/mod.rs')
-rw-r--r--src/data/mod.rs58
1 files changed, 49 insertions, 9 deletions
diff --git a/src/data/mod.rs b/src/data/mod.rs
index 0c61bd7..1465fee 100644
--- a/src/data/mod.rs
+++ b/src/data/mod.rs
@@ -1,4 +1,6 @@
 use serde::{Deserialize, Serialize};
+#[cfg(feature = "ssr")]
+use tokio::sync::Mutex;
 use uuid::Uuid;
 
 #[cfg(feature = "ssr")]
@@ -8,7 +10,7 @@ use std::fs::File;
 #[cfg(feature = "ssr")]
 use std::sync::LazyLock;
 
-use std::{collections::HashMap, path::Path};
+use std::{collections::HashMap, path::Path, sync::Arc};
 
 mod config;
 mod namespace;
@@ -44,29 +46,67 @@ static DATA_LOCK: LazyLock<StormscribeData> = LazyLock::new(|| {
 
     StormscribeData {
         file_lock: lockfile,
-        namespaces: Namespaces::init(&Path::join(&config.data_dir, "namespace/")).unwrap(),
-        pages: Pages::init(&Path::join(&config.data_dir, "pages/")).unwrap(),
+        data_snapshot: Mutex::new(Arc::new(DataSnapshot {
+            namespaces: Namespaces::init(&Path::join(&config.data_dir, "namespace/")).unwrap(),
+            pages: Pages::init(&Path::join(&config.data_dir, "pages/")).unwrap(),
+        })),
     }
 });
 
 #[cfg(feature = "ssr")]
 pub struct StormscribeData {
     file_lock: File,
+    data_snapshot: Mutex<Arc<DataSnapshot>>,
+}
+
+struct DataSnapshot {
     namespaces: Namespaces,
     pages: Pages,
 }
 
+#[derive(Deserialize, Serialize, Clone, Debug)]
+pub struct PageData {
+    path: String,
+    metadata: Option<Page>,
+    content: String,
+}
+
 #[cfg(feature = "ssr")]
 impl StormscribeData {
-    fn get_data() -> &'static Self {
-        &DATA_LOCK
+    async fn get_snapshot() -> Arc<DataSnapshot> {
+        DATA_LOCK.data_snapshot.lock().await.clone()
     }
 
-    pub fn get_namespace() -> Namespace {
-        DATA_LOCK.namespaces.root.clone()
+    pub async fn get_namespace() -> Namespace {
+        StormscribeData::get_snapshot()
+            .await
+            .namespaces
+            .root
+            .clone()
     }
 
-    pub fn get_pages() -> HashMap<PageUuid, Page> {
-        DATA_LOCK.pages.pages.clone()
+    pub async fn get_all_pages() -> HashMap<PageUuid, Page> {
+        StormscribeData::get_snapshot().await.pages.pages.clone()
+    }
+
+    pub async fn get_page_data(page_path: String) -> PageData {
+        let data = Self::get_snapshot().await;
+        let page = data
+            .namespaces
+            .get_page_uuid(&page_path)
+            .map(|page_uuid| data.pages.get_page(&page_uuid))
+            .flatten();
+
+        let content = if let Some(page) = page.cloned() {
+            page.read_content().await.ok()
+        } else {
+            None
+        };
+
+        PageData {
+            path: page_path,
+            metadata: page.cloned(),
+            content: content.unwrap_or(String::new()),
+        }
     }
 }