Trying to get the components to talk to server.

This commit is contained in:
2025-09-06 22:48:58 -04:00
parent 341d1b329c
commit 2895897e2d
3 changed files with 40 additions and 16 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "bard" name = "bard"
version = "0.0.6" version = "0.0.7"
edition = "2024" edition = "2024"
description = "Dioxus components that will display a Tavern blogging system Blog." description = "Dioxus components that will display a Tavern blogging system Blog."
repository = "/CyberMages/tavern" repository = "/CyberMages/tavern"

View File

@ -283,14 +283,14 @@ pub fn PostHeader(title: String, author: String, tags: Vec<String>) -> Element
pub fn BlogPost(slug: String, children: Element) -> Element pub fn BlogPost(slug: String, children: Element) -> Element
{ {
// 1. Fetch the blog post using the slug. // 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 } let url_slug = slug.clone();
Err(e) => async move
{ {
return rsx! { p { "Failed to load post." } p { "{e}" } } get_blog_post(url_slug).await
} }
}; })?;
// Then build the component. // Then build the component.
rsx! rsx!

View File

@ -1,5 +1,10 @@
// Remember that Server functions parameters must not be references. // Remember that Server functions parameters must not be references.
// They are coming from potentially other computers. // 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")] #[cfg(feature = "server")]
use std::sync::Arc; use std::sync::Arc;
@ -22,22 +27,41 @@ static BLOG_DATABASE: OnceCell<Arc<Database>> = OnceCell::const_new();
#[cfg(feature = "server")] #[cfg(feature = "server")]
async fn get_database_instance() -> &'static Arc<Database> async fn get_database_instance<P>(path: P) -> Result<&'static Arc<Database>, ServerFnError>
where P: AsRef<std::path::Path>
{ {
BLOG_DATABASE.get_or_init(|| async { BLOG_DATABASE.get_or_try_init(|| async
let db = Database::open("tavern.db") {
.await let db = Database::open(path).await
.expect("Failed to open database"); .map_err(|e| ServerFnError::new(format!("Failed to open database: {}", e)))?;
Arc::new(db)
Ok(Arc::new(db))
}).await }).await
} }
#[cfg(feature = "server")]
pub async fn init_database<P>(path: P) -> Result<(), ServerFnError>
where P: AsRef<std::path::Path>
{
match get_database_instance(path).await
{
Ok(_) => { Ok(()) }
Err(e) => { Err(e) }
}
}
#[cfg(feature = "server")]
async fn get_database() -> Result<&'static Arc<Database>, ServerFnError>
{
get_database_instance("tavern.db").await
}
#[server] #[server]
pub async fn get_blog_list(categories: Vec<String>) -> Result<Vec<Lore>, ServerFnError> pub async fn get_blog_list(categories: Vec<String>) -> Result<Vec<Lore>, ServerFnError>
{ {
let db = get_database_instance().await; let db = get_database().await?;
let summaries = db.get_tales_summary(&categories).await let summaries = db.get_tales_summary(&categories).await
.map_err(|e| ServerFnError::new(e))?; .map_err(|e| ServerFnError::new(e))?;
@ -49,7 +73,7 @@ pub async fn get_blog_list(categories: Vec<String>) -> Result<Vec<Lore>, ServerF
#[server] #[server]
pub async fn get_blog_post(slug: String) -> Result<Tale, ServerFnError> pub async fn get_blog_post(slug: String) -> Result<Tale, ServerFnError>
{ {
let db = get_database_instance().await; let db = get_database().await?;
let tale = db.get_tale_by_slug(&slug).await let tale = db.get_tale_by_slug(&slug).await
.map_err(|e| ServerFnError::new(e))?; .map_err(|e| ServerFnError::new(e))?;
@ -61,7 +85,7 @@ pub async fn get_blog_post(slug: String) -> Result<Tale, ServerFnError>
#[server] #[server]
pub async fn get_author(handle: String) -> Result<Adventurer, ServerFnError> pub async fn get_author(handle: String) -> Result<Adventurer, ServerFnError>
{ {
let db = get_database_instance().await; let db = get_database().await?;
let author = db.get_adventurer(&handle).await let author = db.get_adventurer(&handle).await
.map_err(|e| ServerFnError::new(e))?; .map_err(|e| ServerFnError::new(e))?;