From 2895897e2d2eb298f597c9a8c529baca1b1ee687 Mon Sep 17 00:00:00 2001 From: Myrddin Dundragon Date: Sat, 6 Sep 2025 22:48:58 -0400 Subject: [PATCH] Trying to get the components to talk to server. --- bard/Cargo.toml | 2 +- bard/src/components.rs | 10 +++++----- bard/src/server.rs | 44 ++++++++++++++++++++++++++++++++---------- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/bard/Cargo.toml b/bard/Cargo.toml index 29fe457..5d10e57 100644 --- a/bard/Cargo.toml +++ b/bard/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bard" -version = "0.0.6" +version = "0.0.7" edition = "2024" description = "Dioxus components that will display a Tavern blogging system Blog." repository = "/CyberMages/tavern" diff --git a/bard/src/components.rs b/bard/src/components.rs index e1570e6..9fa3803 100644 --- a/bard/src/components.rs +++ b/bard/src/components.rs @@ -283,14 +283,14 @@ pub fn PostHeader(title: String, author: String, tags: Vec) -> Element pub fn BlogPost(slug: String, children: Element) -> Element { // 1. Fetch the blog post using the slug. - let post = match use_server_future(move || get_blog_post(slug.clone())) + let post = use_server_future(move || { - Ok(post) => { post } - Err(e) => + let url_slug = slug.clone(); + async move { - return rsx! { p { "Failed to load post." } p { "{e}" } } + get_blog_post(url_slug).await } - }; + })?; // Then build the component. rsx! diff --git a/bard/src/server.rs b/bard/src/server.rs index ded62e2..c4f4e05 100644 --- a/bard/src/server.rs +++ b/bard/src/server.rs @@ -1,5 +1,10 @@ // Remember that Server functions parameters must not be references. // They are coming from potentially other computers. +// +// Server functions must be: +// * Be an async function +// * Have arguments and a return type that both implement serialize and deserialize (with serde). +// * Return a Result with an error type of ServerFnError #[cfg(feature = "server")] use std::sync::Arc; @@ -22,14 +27,33 @@ static BLOG_DATABASE: OnceCell> = OnceCell::const_new(); #[cfg(feature = "server")] -async fn get_database_instance() -> &'static Arc +async fn get_database_instance

(path: P) -> Result<&'static Arc, ServerFnError> + where P: AsRef { - BLOG_DATABASE.get_or_init(|| async { - let db = Database::open("tavern.db") - .await - .expect("Failed to open database"); - Arc::new(db) - }).await + BLOG_DATABASE.get_or_try_init(|| async + { + let db = Database::open(path).await + .map_err(|e| ServerFnError::new(format!("Failed to open database: {}", e)))?; + + Ok(Arc::new(db)) + }).await +} + +#[cfg(feature = "server")] +pub async fn init_database

(path: P) -> Result<(), ServerFnError> + where P: AsRef +{ + match get_database_instance(path).await + { + Ok(_) => { Ok(()) } + Err(e) => { Err(e) } + } +} + +#[cfg(feature = "server")] +async fn get_database() -> Result<&'static Arc, ServerFnError> +{ + get_database_instance("tavern.db").await } @@ -37,7 +61,7 @@ async fn get_database_instance() -> &'static Arc #[server] pub async fn get_blog_list(categories: Vec) -> Result, ServerFnError> { - let db = get_database_instance().await; + let db = get_database().await?; let summaries = db.get_tales_summary(&categories).await .map_err(|e| ServerFnError::new(e))?; @@ -49,7 +73,7 @@ pub async fn get_blog_list(categories: Vec) -> Result, ServerF #[server] pub async fn get_blog_post(slug: String) -> Result { - let db = get_database_instance().await; + let db = get_database().await?; let tale = db.get_tale_by_slug(&slug).await .map_err(|e| ServerFnError::new(e))?; @@ -61,7 +85,7 @@ pub async fn get_blog_post(slug: String) -> Result #[server] pub async fn get_author(handle: String) -> Result { - let db = get_database_instance().await; + let db = get_database().await?; let author = db.get_adventurer(&handle).await .map_err(|e| ServerFnError::new(e))?;