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:
@@ -56,7 +56,7 @@ use utoipa_swagger_ui::SwaggerUi;
|
|||||||
struct ApiDoc;
|
struct ApiDoc;
|
||||||
|
|
||||||
pub fn add_routes(router: Router<AppState>) -> Router<AppState> {
|
pub fn add_routes(router: Router<AppState>) -> Router<AppState> {
|
||||||
router
|
let mut router = router
|
||||||
// Status endpoints
|
// Status endpoints
|
||||||
.route("/api/status", get(status::handle_status))
|
.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/meta", get(item::handle_get_item_latest_meta))
|
||||||
.route("/api/item/latest/content", get(item::handle_get_item_latest_content))
|
.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}/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")]
|
||||||
#[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> {
|
pub fn add_docs_routes(router: Router<AppState>) -> Router<AppState> {
|
||||||
|
|||||||
@@ -99,19 +99,24 @@ async fn run_server(
|
|||||||
.route("/mcp", post(mcp::handle_mcp_request))
|
.route("/mcp", post(mcp::handle_mcp_request))
|
||||||
.with_state(state.clone());
|
.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
|
// Create the app with documentation routes open and others protected
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
// Add documentation routes without authentication
|
// Add documentation routes without authentication
|
||||||
.merge(api::add_docs_routes(Router::new()))
|
.merge(api::add_docs_routes(Router::new()))
|
||||||
// Add API, pages, and MCP routes with authentication
|
// Add API, pages, and MCP routes with authentication
|
||||||
.merge(
|
.merge(protected_router)
|
||||||
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())))
|
|
||||||
)
|
|
||||||
// Apply state to all routes
|
// Apply state to all routes
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
// Add other middleware layers to all routes
|
// Add other middleware layers to all routes
|
||||||
|
|||||||
Reference in New Issue
Block a user