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

View File

@@ -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
}

View File

@@ -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<std::convert::Infallible> {
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<Router> {
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))
}