summary refs log tree commit diff
path: root/src/data/mod.rs
diff options
context:
space:
mode:
authortempest <git@ashen.earth>2025-04-15 00:08:12 -0600
committertempest <git@ashen.earth>2025-04-15 00:08:12 -0600
commitb856f12cf422b96c37c12df3d7829e4d15ef4453 (patch)
tree4547df3ec3d347715292a860a5a63207fe428de3 /src/data/mod.rs
parent128cc42557c8d7da46c63a40ea4469ed0eb7f26d (diff)
Can find content data
Diffstat (limited to 'src/data/mod.rs')
-rw-r--r--src/data/mod.rs73
1 files changed, 71 insertions, 2 deletions
diff --git a/src/data/mod.rs b/src/data/mod.rs
index 4767914..0c61bd7 100644
--- a/src/data/mod.rs
+++ b/src/data/mod.rs
@@ -1,3 +1,72 @@
-pub mod config;
-pub mod content;
+use serde::{Deserialize, Serialize};
+use uuid::Uuid;
 
+#[cfg(feature = "ssr")]
+use fs2::FileExt;
+#[cfg(feature = "ssr")]
+use std::fs::File;
+#[cfg(feature = "ssr")]
+use std::sync::LazyLock;
+
+use std::{collections::HashMap, path::Path};
+
+mod config;
+mod namespace;
+mod page;
+
+use config::Config;
+pub use namespace::{Namespace, Namespaces};
+pub use page::{Page, Pages};
+
+#[derive(Hash, PartialEq, Eq, Clone, Debug, Deserialize, Serialize)]
+pub struct PageUuid(Uuid);
+
+#[cfg(feature = "ssr")]
+pub static CONFIG: LazyLock<Config> =
+    LazyLock::new(|| Config::read_from_file().expect("Could not open config file"));
+
+#[cfg(feature = "ssr")]
+static DATA_LOCK: LazyLock<StormscribeData> = LazyLock::new(|| {
+    let config = &CONFIG;
+    let lock_path = Path::join(&config.data_dir, ".lock");
+    let lockfile = std::fs::OpenOptions::new()
+        .read(true)
+        .write(true)
+        .create(true)
+        .open(&lock_path)
+        .map_err(|_| "Could not open data directory".to_string())
+        .unwrap();
+
+    lockfile
+        .try_lock_exclusive()
+        .map_err(|_| "Could not lock data directory".to_string())
+        .unwrap();
+
+    StormscribeData {
+        file_lock: lockfile,
+        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,
+    namespaces: Namespaces,
+    pages: Pages,
+}
+
+#[cfg(feature = "ssr")]
+impl StormscribeData {
+    fn get_data() -> &'static Self {
+        &DATA_LOCK
+    }
+
+    pub fn get_namespace() -> Namespace {
+        DATA_LOCK.namespaces.root.clone()
+    }
+
+    pub fn get_pages() -> HashMap<PageUuid, Page> {
+        DATA_LOCK.pages.pages.clone()
+    }
+}