Move server setup into router_setup
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 8s
Push Workflows / docs (push) Successful in 33s
Push Workflows / clippy (push) Successful in 31s
Push Workflows / test (push) Successful in 57s
Push Workflows / build (push) Successful in 1m25s
Push Workflows / nix-build (push) Successful in 5m17s

Remove tokio (dioxus::serve provides a runtime)
This commit is contained in:
2026-06-27 18:51:44 -04:00
parent d52c4cbe9e
commit f2a1296454
4 changed files with 18 additions and 30 deletions

1
Cargo.lock generated
View File

@@ -2427,7 +2427,6 @@ dependencies = [
"rand 0.10.1", "rand 0.10.1",
"serde", "serde",
"thiserror 2.0.18", "thiserror 2.0.18",
"tokio",
"tracing", "tracing",
] ]

View File

@@ -24,7 +24,6 @@ pbkdf2 = { version = "0.13.0", optional = true, features = ["getrandom", "phc"]
rand = "0.10.1" rand = "0.10.1"
serde = { version = "1.0.228", features = ["derive"] } serde = { version = "1.0.228", features = ["derive"] }
thiserror = "2.0.18" thiserror = "2.0.18"
tokio = { version = "1.52.3", optional = true, features = ["rt-multi-thread"] }
tracing = "0.1.44" tracing = "0.1.44"
[features] [features]
@@ -40,7 +39,6 @@ server = [
"dep:dotenvy", "dep:dotenvy",
"dep:fred", "dep:fred",
"dep:pbkdf2", "dep:pbkdf2",
"dep:tokio",
] ]
# Disabled until supported # Disabled until supported

View File

@@ -28,9 +28,8 @@ fn main() {
fn main() -> std::process::ExitCode { fn main() -> std::process::ExitCode {
tracing_setup(); tracing_setup();
if let Err(e) = server::main() { let Err(e) = server::main();
tracing::error!("Server main failed:\n{e}"); tracing::error!("Server main failed:\n{e}");
}
std::process::ExitCode::FAILURE std::process::ExitCode::FAILURE
} }

View File

@@ -1,41 +1,33 @@
use tokio::runtime::Runtime; use dioxus::fullstack::axum::Router;
use crate::App; use crate::App;
use crate::server::{config, database, key_val_store}; use crate::server::{config, database, key_val_store};
use crate::util::error::{Contextualize, Error, Result}; use crate::util::error::{Contextualize, Error, Result};
pub fn main() -> Result<()> { pub fn main() -> Result<std::convert::Infallible> {
if let Err(e) = dotenvy::dotenv() { if let Err(e) = dotenvy::dotenv() {
tracing::warn!("Error reading .env: {e}"); 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<Router> {
tracing::debug!("Loading configuration..."); tracing::debug!("Loading configuration...");
let config = config::load_config() let config = config::load_config()
.map_err(|e| Error::message_here(e.to_string())) .map_err(|e| Error::message_here(e.to_string()))
.err_context("Failed to load config")?; .err_context("Failed to load config")?;
// `dioxus::launch` creates its own runtime, and starting a runtime inside of a runtime isn't let _db_pool = database::setup(config.database.connection_uri())
// allowed. Therefore, this function can't be made async, and we must manually create a runtime .await
// for any async setup tasks .err_context("Failed database setup")?;
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, _key_val_pool) = setup_rt.block_on(async { let _key_val_pool = key_val_store::setup(&config.key_val_store.connection_uri())
let db_pool = database::setup(config.database.connection_uri()) .await
.await .err_context("Failed key-value store setup")?;
.err_context("Failed database setup")?;
let key_val_pool = key_val_store::setup(&config.key_val_store.connection_uri()) tracing::info!("Setup complete, returning Router...");
.await Ok(dioxus::server::router(App))
.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"))
} }