From 39c3375cf5cd8c22d3d0e1c84ad5f00360b84cf2 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Wed, 10 Sep 2025 10:00:30 -0300 Subject: [PATCH] refactor: Simplify server router merging with conditional compilation Co-authored-by: aider (openai/andrew/openrouter/sonoma-sky-alpha) --- src/modes/server/api/mod.rs | 15 +++++++++------ src/modes/server/mod.rs | 21 +++++++++++++-------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/modes/server/api/mod.rs b/src/modes/server/api/mod.rs index 27d00ea..cbb9dcb 100644 --- a/src/modes/server/api/mod.rs +++ b/src/modes/server/api/mod.rs @@ -56,7 +56,7 @@ use utoipa_swagger_ui::SwaggerUi; struct ApiDoc; pub fn add_routes(router: Router) -> Router { - router + let mut router = router // Status endpoints .route("/api/status", get(status::handle_status)) @@ -65,11 +65,14 @@ pub fn add_routes(router: Router) -> Router { .route("/api/item/latest/meta", get(item::handle_get_item_latest_meta)) .route("/api/item/latest/content", get(item::handle_get_item_latest_content)) .route("/api/item/{item_id}/meta", get(item::handle_get_item_meta)) - .route("/api/item/{item_id}/content", get(item::handle_get_item_content)) - - // MCP endpoints - #[cfg(feature = "mcp")] - .route("/mcp/sse", get(mcp::handle_mcp_sse)) + .route("/api/item/{item_id}/content", get(item::handle_get_item_content)); + + #[cfg(feature = "mcp")] + { + router = router.route("/mcp/sse", get(mcp::handle_mcp_sse)); + } + + router } pub fn add_docs_routes(router: Router) -> Router { diff --git a/src/modes/server/mod.rs b/src/modes/server/mod.rs index b1ced07..29a4fdb 100644 --- a/src/modes/server/mod.rs +++ b/src/modes/server/mod.rs @@ -99,19 +99,24 @@ async fn run_server( .route("/mcp", post(mcp::handle_mcp_request)) .with_state(state.clone()); + let mut protected_router = Router::new() + .merge(api::add_routes(Router::new())) + .merge(pages::add_routes(Router::new())); + + #[cfg(feature = "mcp")] + { + protected_router = protected_router.merge(mcp_router); + } + + let protected_router = protected_router + .layer(axum::middleware::from_fn(create_auth_middleware(config.password.clone(), config.password_hash.clone()))); + // Create the app with documentation routes open and others protected let app = Router::new() // Add documentation routes without authentication .merge(api::add_docs_routes(Router::new())) // Add API, pages, and MCP routes with authentication - .merge( - Router::new() - .merge(api::add_routes(Router::new())) - .merge(pages::add_routes(Router::new())) - #[cfg(feature = "mcp")] - .merge(mcp_router) - .layer(axum::middleware::from_fn(create_auth_middleware(config.password.clone(), config.password_hash.clone()))) - ) + .merge(protected_router) // Apply state to all routes .with_state(state) // Add other middleware layers to all routes