diff options
author | tempest <git@ashen.earth> | 2025-04-13 16:40:05 -0600 |
---|---|---|
committer | tempest <git@ashen.earth> | 2025-04-13 16:40:05 -0600 |
commit | 128cc42557c8d7da46c63a40ea4469ed0eb7f26d (patch) | |
tree | 6b70766ec46061240a1d42fde100187697d60bc8 | |
parent | e487e052b3cfcf90f6b831052e30323d3e744526 (diff) |
Compiles, cannot find data context
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Cargo.toml | 7 | ||||
-rw-r--r-- | src/actions/mod.rs | 1 | ||||
-rw-r--r-- | src/actions/page.rs | 12 | ||||
-rw-r--r-- | src/components/app.rs | 19 | ||||
-rw-r--r-- | src/components/renderer/mod.rs | 18 | ||||
-rw-r--r-- | src/data/config.rs | 1 | ||||
-rw-r--r-- | src/data/content.rs | 25 | ||||
-rw-r--r-- | src/lib.rs | 3 | ||||
-rw-r--r-- | src/main.rs | 8 |
10 files changed, 59 insertions, 37 deletions
diff --git a/.gitignore b/.gitignore index 2ddc3a8..0f7d43e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /target /.generated-style.scss +/.sass-cache/ +/data/ diff --git a/Cargo.toml b/Cargo.toml index 7fa8aa3..b0fe882 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,8 +19,8 @@ wasm-bindgen = { version = "=0.2.100", optional = true } serde = { version = "^1.0.219", features = ["derive"] } stylance = { version = "0.5.5" } toml = { version = "0.8.20", features = ["parse"], optional = true } -uuid = { version = "1.16.0", features = ["v4"], optional = true } -chrono = { version = "0.4.40", optional = true } +uuid = { version = "1.16.0" } +chrono = { version = "0.4.40" } fs2 = { version = "0.4.3", optional = true} futures = { version = "0.3.31", optional = true} @@ -37,13 +37,12 @@ ssr = [ "dep:leptos_axum", "dep:tokio-stream", "dep:toml", - "dep:uuid", - "dep:chrono", "dep:fs2", "dep:futures", "leptos/ssr", "leptos_meta/ssr", "leptos_router/ssr", + "uuid/v4", ] # Defines a size-optimized profile for the WASM bundle in release mode diff --git a/src/actions/mod.rs b/src/actions/mod.rs new file mode 100644 index 0000000..79d2861 --- /dev/null +++ b/src/actions/mod.rs @@ -0,0 +1 @@ +pub mod page; diff --git a/src/actions/page.rs b/src/actions/page.rs index c51e6bc..8d204f5 100644 --- a/src/actions/page.rs +++ b/src/actions/page.rs @@ -1,2 +1,12 @@ +use leptos::prelude::*; + #[server] -pub async fn get_page_content() +pub async fn get_page_content(url_path: String) -> Result<(), ServerFnError> { + use crate::data::content::ContentController; + let content_controller = expect_context::<ContentController>(); + let content_snapshot = content_controller.get_snapshot().await; + let page_uuid = content_snapshot.page_paths.get(&url_path); + let page = content_snapshot.pages.get(&page_uuid.unwrap()); + + return Ok(()) +} diff --git a/src/components/app.rs b/src/components/app.rs index 5033359..a7ac5bd 100644 --- a/src/components/app.rs +++ b/src/components/app.rs @@ -63,6 +63,8 @@ struct PageParams { // Renders a page #[component] fn PageRender() -> impl IntoView { + use crate::actions::page::get_page_content; + let params = use_params::<PageParams>(); let page_path = params.read() @@ -72,8 +74,23 @@ fn PageRender() -> impl IntoView { .unwrap_or("Unknown path".to_string())) .unwrap_or("Could not read params".to_string()); + let page_data = Resource::new( + move || page_path.clone(), + |page_path| get_page_content(page_path) + ); + view! { - <WikiPage url_path=page_path /> + <Suspense + fallback=move || view! { <p>"Loading..."</p> } + > + {move || Suspend::new(async move { + let data = page_data.await; + match data { + Ok(_) => view! {<p>Loaded</p>}.into_any(), + Err(_) => view! {<p>Error</p>}.into_any(), + } + })} + </Suspense> }.into_any() } diff --git a/src/components/renderer/mod.rs b/src/components/renderer/mod.rs index 6bc029a..68aac9c 100644 --- a/src/components/renderer/mod.rs +++ b/src/components/renderer/mod.rs @@ -1,24 +1,12 @@ use leptos::prelude::*; use leptos::{component, view, IntoView}; - -// use crate::data::content::ContentController; +use crate::data::content::{Page,Namespace}; #[component] pub fn WikiPage( - url_path: String, + page_data: Page, + parent_namespaces: Vec<Namespace>, ) -> impl IntoView { - // let content_controller = use_context::<ContentController>().unwrap(); - - // let _snapshot = Resource::new( - // move || (url_path.clone(), content_controller.clone()), - // |url_path: String| { - // let page_path = url_path.clone(); - // - // async move { - // let content = content_controller.get_snapshot().await; - // let page = content.page_paths.get(&page_path); - // } - // }); view! { <h1>Article (Viewing)</h1> diff --git a/src/data/config.rs b/src/data/config.rs index bce5c94..9a17077 100644 --- a/src/data/config.rs +++ b/src/data/config.rs @@ -20,6 +20,7 @@ pub struct Config { footer_copyright: Option<String> } +#[cfg(feature = "ssr")] impl Config { pub fn read_from_file<P: AsRef<Path>>(path: P) -> Result<Self, String> { let config_contents = fs::read_to_string(&path) diff --git a/src/data/content.rs b/src/data/content.rs index a628be0..4a39967 100644 --- a/src/data/content.rs +++ b/src/data/content.rs @@ -1,24 +1,31 @@ +#[cfg(feature="ssr")] use std::fs::File; use std::collections::HashMap; +#[cfg(feature="ssr")] use std::io::{BufRead, BufReader}; use std::path::{PathBuf, Path}; use std::sync::Arc; +#[cfg(feature="ssr")] use tokio::sync::RwLock; use chrono::{DateTime, Utc}; use leptos::prelude::StorageAccess; use serde::Deserialize; use uuid::Uuid; +#[cfg(feature="ssr")] use fs2::FileExt; +#[cfg(feature="ssr")] use tokio::runtime; +#[cfg(feature="ssr")] use tokio_stream::wrappers::ReadDirStream; +#[cfg(feature="ssr")] use futures::stream::StreamExt; #[derive(Hash, PartialEq, Eq, Clone)] -struct PageUuid(Uuid); +pub struct PageUuid(Uuid); #[derive(Hash, PartialEq, Eq, Clone)] -struct NamespaceUuid(Uuid); +pub struct NamespaceUuid(Uuid); #[derive(Hash, PartialEq, Eq, Clone)] -struct MediaUuid(Uuid); +pub struct MediaUuid(Uuid); pub struct ContentSnapshot { pub pages: HashMap<PageUuid, Page>, @@ -58,6 +65,7 @@ struct Media { used_on: Vec<PageUuid>, } +#[cfg(feature="ssr")] #[derive(Clone)] pub struct ContentController { snapshot: Arc<RwLock<Box<Arc<ContentSnapshot>>>>, @@ -66,7 +74,7 @@ pub struct ContentController { #[cfg(feature = "ssr")] impl ContentController { - pub fn init(data_dir: PathBuf) -> Result<Self, String> { + pub async fn init(data_dir: PathBuf) -> Result<Self, String> { let lock_path = Path::join(&data_dir, ".lock"); let lockfile = std::fs::OpenOptions::new() .read(true).write(true).create(true) @@ -76,12 +84,8 @@ impl ContentController { lockfile.try_lock_exclusive() .map_err(|_| "Could not lock data directory".to_string())?; - let runtime = runtime::Builder::new_multi_thread() - .build() - .map_err(|_| "Could not start async runtime".to_string())?; - // Read the things - let snapshot = runtime.block_on(Self::read_data(&data_dir))?; + let snapshot = Self::read_data(&data_dir).await?; Ok(Self { lock: Arc::new(lockfile), @@ -153,7 +157,7 @@ impl ContentController { }) .unzip(); - let pages_dir = Path::join(&data_dir, "pages/id"); + let pages_dir = Path::join(&data_dir, "pages"); let pages = fs::read_dir(&pages_dir).await .map_err(|_| "Could not open pages data directory".to_string()) .map(|dir_entries| { ReadDirStream::new(dir_entries) })? @@ -205,6 +209,7 @@ impl ContentController { const METADATA_DIVIDER : &'static str = "<!-- trans rights ~ath&+ -->"; +#[cfg(feature = "ssr")] impl Page { async fn init_from_file(path: &PathBuf, pagedata_cache: &HashMap::<PageUuid, (String, NamespaceUuid)>) -> Option<Self> { let mut reader = BufReader::new(File::open(path).ok()?); diff --git a/src/lib.rs b/src/lib.rs index 3ae130e..0f5c708 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,5 @@ pub mod components; - -#[cfg(feature = "ssr")] +pub mod actions; pub mod data; #[cfg(feature = "hydrate")] diff --git a/src/main.rs b/src/main.rs index 13512c5..d3c4f58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ async fn main() { use leptos::prelude::*; use leptos_axum::{generate_route_list, LeptosRoutes}; use stormscribe::components::app::*; - // use stormscribe::data::content::ContentController; + use stormscribe::data::content::ContentController; let conf = get_configuration(None).unwrap(); let addr = conf.leptos_options.site_addr; @@ -15,15 +15,15 @@ async fn main() { // Generate the list of routes in your Leptos App let routes = generate_route_list(App); - // let data_dir = std::path::Path::join(&std::env::current_dir().unwrap(), "data"); - // let content_controller = ContentController::init(data_dir).unwrap(); + let data_dir = std::path::Path::join(&std::env::current_dir().unwrap(), "data"); + let content_controller = ContentController::init(data_dir).await.unwrap(); let app = Router::new() .route("/", get(|| async { Redirect::temporary("/~/") })) .leptos_routes_with_context(&leptos_options, routes, move || { - // provide_context(content_controller.clone()); + provide_context(content_controller.clone()); }, { let leptos_options = leptos_options.clone(); move || shell(leptos_options.clone()) |