Move router setup to build_router
Some checks failed
Push Workflows / rustfmt (push) Failing after 5s
Push Workflows / tailwind-build (push) Successful in 5s
Push Workflows / docs (push) Successful in 19s
Push Workflows / clippy (push) Successful in 17s
Push Workflows / test (push) Successful in 24s
Push Workflows / build (push) Successful in 46s
Push Workflows / nix-build (push) Successful in 5m11s

This commit is contained in:
2026-07-19 10:24:54 -04:00
parent a2c754de2c
commit 328b23f305

View File

@@ -9,11 +9,35 @@ use tower_http::services::ServeFile;
use crate::App; use crate::App;
use crate::app::LOGO_ICO; use crate::app::LOGO_ICO;
use crate::server::{ use crate::server::{
auth::build_auth_layer, config::Config, database, key_val_store, auth::build_auth_layer, config::Config, database::{self, DbPool}, key_val_store::{self, KeyValPool},
require_auth_mw::require_auth_middleware, require_auth_mw::require_auth_middleware,
}; };
use crate::util::error::{Contextualize, Error, ErrorType, Result}; use crate::util::error::{Contextualize, Error, ErrorType, Result};
pub fn build_router(config: Config, db_pool: DbPool, key_val_pool: KeyValPool) -> Router {
let favicon_path = {
// Resolve the favicon path
let asset_path = LOGO_ICO.resolve();
// If the asset path starts with "/", strip it. Otherwise it behaves as an "absolute path"
// and replaces the base path in the join operation. This is necessary because Dioxus will
// produce a path like "/assets/" in the call to `resolve`
let asset_path_rel = asset_path.strip_prefix("/").unwrap_or(&asset_path);
config.server.public_path.join(asset_path_rel)
};
let auth_layer = build_auth_layer(db_pool.clone(), key_val_pool, config.auth.cookies_secure);
Router::new()
.serve_dioxus_application(ServeConfig::new(), App)
.layer(from_fn(require_auth_middleware))
.layer(Extension(config))
.layer(Extension(db_pool))
.layer(auth_layer)
.nest_service("/favicon.ico", ServeFile::new(favicon_path))
}
pub async fn main() -> Result<std::convert::Infallible> { pub async 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}");
@@ -43,33 +67,10 @@ pub async fn main() -> Result<std::convert::Infallible> {
.await .await
.err_context("Failed key-value store setup")?; .err_context("Failed key-value store setup")?;
let favicon_path = {
// Resolve the favicon path
let asset_path = LOGO_ICO.resolve();
// If the asset path starts with "/", strip it. Otherwise it behaves as an "absolute path"
// and replaces the base path in the join operation. This is necessary because Dioxus will
// produce a path like "/assets/" in the call to `resolve`
let asset_path_rel = asset_path.strip_prefix("/").unwrap_or(&asset_path);
config.server.public_path.join(asset_path_rel)
};
tracing::debug!("Favicon path: {}", favicon_path.display());
let auth_layer = build_auth_layer(db_pool.clone(), key_val_pool, config.auth.cookies_secure);
let addr = config.server.serve_addr(); let addr = config.server.serve_addr();
tracing::info!("Setup complete, building router..."); tracing::info!("Setup complete, building router...");
let router = build_router(config, db_pool, key_val_pool);
let router = Router::new()
.serve_dioxus_application(ServeConfig::new(), App)
.layer(from_fn(require_auth_middleware))
.layer(Extension(config))
.layer(Extension(db_pool))
.layer(auth_layer)
.nest_service("/favicon.ico", ServeFile::new(favicon_path));
tracing::info!("Listening on {addr}..."); tracing::info!("Listening on {addr}...");
let listener = TcpListener::bind(addr) let listener = TcpListener::bind(addr)