// Remember that Server functions parameters must not be references. // They are coming from potentially other computers. #[cfg(feature = "server")] use std::sync::Arc; use dioxus::prelude::*; #[cfg(feature = "server")] use tokio::sync::OnceCell; use tavern::{Adventurer, Lore, Tale}; #[cfg(feature = "server")] use tavern::Database; #[cfg(feature = "server")] static BLOG_DATABASE: OnceCell> = OnceCell::const_new(); #[cfg(feature = "server")] async fn get_database_instance() -> &'static Arc { BLOG_DATABASE.get_or_init(|| async { let db = Database::open("tavern.db") .await .expect("Failed to open database"); Arc::new(db) }).await } #[server] pub async fn get_blog_list(categories: Vec) -> Result, ServerFnError> { let db = get_database_instance().await; let summaries = db.get_tales_summary(&categories).await .map_err(|e| ServerFnError::new(e))?; Ok(summaries) } #[server] pub async fn get_blog_post(slug: String) -> Result { let db = get_database_instance().await; let tale = db.get_tale_by_slug(&slug).await .map_err(|e| ServerFnError::new(e))?; tale.ok_or(ServerFnError::new(format!("Post {} not found", slug))) } #[server] pub async fn get_author(handle: String) -> Result { let db = get_database_instance().await; let author = db.get_adventurer(&handle).await .map_err(|e| ServerFnError::new(e))?; author.ok_or(ServerFnError::new(format!("Author {} not found.", handle))) }