Trying to get the components to talk to server.
This commit is contained in:
@ -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"
|
||||
|
||||
@ -283,14 +283,14 @@ pub fn PostHeader(title: String, author: String, tags: Vec<String>) -> 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!
|
||||
|
||||
@ -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<Arc<Database>> = OnceCell::const_new();
|
||||
|
||||
|
||||
#[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 {
|
||||
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<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
|
||||
}
|
||||
|
||||
|
||||
@ -37,7 +61,7 @@ async fn get_database_instance() -> &'static Arc<Database>
|
||||
#[server]
|
||||
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
|
||||
.map_err(|e| ServerFnError::new(e))?;
|
||||
@ -49,7 +73,7 @@ pub async fn get_blog_list(categories: Vec<String>) -> Result<Vec<Lore>, ServerF
|
||||
#[server]
|
||||
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
|
||||
.map_err(|e| ServerFnError::new(e))?;
|
||||
@ -61,7 +85,7 @@ pub async fn get_blog_post(slug: String) -> Result<Tale, ServerFnError>
|
||||
#[server]
|
||||
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
|
||||
.map_err(|e| ServerFnError::new(e))?;
|
||||
|
||||
Reference in New Issue
Block a user