refactor: Simplify server router merging with conditional compilation

Co-authored-by: aider (openai/andrew/openrouter/sonoma-sky-alpha) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-09-10 10:00:30 -03:00
parent 8f3f6c05db
commit 39c3375cf5
2 changed files with 22 additions and 14 deletions

View File

@@ -56,7 +56,7 @@ use utoipa_swagger_ui::SwaggerUi;
struct ApiDoc;
pub fn add_routes(router: Router<AppState>) -> Router<AppState> {
router
let mut router = router
// Status endpoints
.route("/api/status", get(status::handle_status))
@@ -65,11 +65,14 @@ pub fn add_routes(router: Router<AppState>) -> Router<AppState> {
.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))
.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))
{
router = router.route("/mcp/sse", get(mcp::handle_mcp_sse));
}
router
}
pub fn add_docs_routes(router: Router<AppState>) -> Router<AppState> {

View File

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