diff --git a/Cargo.lock b/Cargo.lock index 95b72a0..daa7c4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2427,7 +2427,6 @@ dependencies = [ "rand 0.10.1", "serde", "thiserror 2.0.18", - "tokio", "tracing", ] diff --git a/Cargo.toml b/Cargo.toml index 8ebe95b..07770e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ pbkdf2 = { version = "0.13.0", optional = true, features = ["getrandom", "phc"] rand = "0.10.1" serde = { version = "1.0.228", features = ["derive"] } thiserror = "2.0.18" -tokio = { version = "1.52.3", optional = true, features = ["rt-multi-thread"] } tracing = "0.1.44" [features] @@ -40,7 +39,6 @@ server = [ "dep:dotenvy", "dep:fred", "dep:pbkdf2", - "dep:tokio", ] # Disabled until supported diff --git a/src/main.rs b/src/main.rs index 8a0edc8..80cbf7c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,9 +28,8 @@ fn main() { fn main() -> std::process::ExitCode { tracing_setup(); - if let Err(e) = server::main() { - tracing::error!("Server main failed:\n{e}"); - } + let Err(e) = server::main(); + tracing::error!("Server main failed:\n{e}"); std::process::ExitCode::FAILURE } diff --git a/src/server/main.rs b/src/server/main.rs index e961f97..1bb4e27 100644 --- a/src/server/main.rs +++ b/src/server/main.rs @@ -1,41 +1,33 @@ -use tokio::runtime::Runtime; +use dioxus::fullstack::axum::Router; use crate::App; use crate::server::{config, database, key_val_store}; use crate::util::error::{Contextualize, Error, Result}; -pub fn main() -> Result<()> { +pub fn main() -> Result { if let Err(e) = dotenvy::dotenv() { tracing::warn!("Error reading .env: {e}"); } + // `Ok(...?)` is because `dioxus::serve` expects an `anyhow::Result` + dioxus::serve(async move || Ok(router_setup().await?)); +} + +/// Set up the axum Router +async fn router_setup() -> Result { tracing::debug!("Loading configuration..."); let config = config::load_config() .map_err(|e| Error::message_here(e.to_string())) .err_context("Failed to load config")?; - // `dioxus::launch` creates its own runtime, and starting a runtime inside of a runtime isn't - // allowed. Therefore, this function can't be made async, and we must manually create a runtime - // for any async setup tasks - tracing::debug!("Starting setup runtime..."); - let setup_rt = Runtime::new() - .map_err(|e| Error::message_here(e.to_string())) - .err_context("Failed to create tokio runtime for server setup")?; + let _db_pool = database::setup(config.database.connection_uri()) + .await + .err_context("Failed database setup")?; - let (_db_pool, _key_val_pool) = setup_rt.block_on(async { - let db_pool = database::setup(config.database.connection_uri()) - .await - .err_context("Failed database setup")?; + let _key_val_pool = key_val_store::setup(&config.key_val_store.connection_uri()) + .await + .err_context("Failed key-value store setup")?; - let key_val_pool = key_val_store::setup(&config.key_val_store.connection_uri()) - .await - .err_context("Failed key-value store setup")?; - - Ok::<_, Error>((db_pool, key_val_pool)) - })?; - - tracing::info!("Setup complete, launching web server..."); - dioxus::launch(App); - - Err(Error::message_here("Web server exited")) + tracing::info!("Setup complete, returning Router..."); + Ok(dioxus::server::router(App)) }