microsandbox_server/
route.rs

1//! Router configuration for the microsandbox server.
2//!
3//! This module handles:
4//! - API route definitions
5//! - Router configuration and setup
6//! - Request routing and handling
7//!
8//! The module provides:
9//! - Router creation and configuration
10//! - Route handlers and middleware integration
11//! - State management for routes
12
13use axum::{
14    middleware,
15    routing::{get, post},
16    Router,
17};
18
19use crate::{handler, middleware as app_middleware, state::AppState};
20
21//--------------------------------------------------------------------------------------------------
22// Functions
23//--------------------------------------------------------------------------------------------------
24
25/// Create a new router with the given state
26pub fn create_router(state: AppState) -> Router {
27    // Create REST API routes - only health endpoint remains here
28    let rest_api = Router::new().route("/health", get(handler::health));
29
30    // Create JSON-RPC routes with authentication - a single endpoint that handles all RPC methods
31    // This now mirrors the structure used in microsandbox-portal
32    let rpc_api = Router::new()
33        .route("/", post(handler::json_rpc_handler))
34        .layer(middleware::from_fn_with_state(
35            state.clone(),
36            app_middleware::auth_middleware,
37        ));
38
39    // Create MCP routes - separate endpoint for Model Context Protocol
40    // Uses smart auth middleware that handles protocol vs tool methods differently
41    let mcp_api =
42        Router::new()
43            .route("/", post(handler::mcp_handler))
44            .layer(middleware::from_fn_with_state(
45                state.clone(),
46                app_middleware::mcp_smart_auth_middleware,
47            ));
48
49    // Combine all routes with logging middleware
50    Router::new()
51        .nest("/api/v1", rest_api)
52        .nest("/api/v1/rpc", rpc_api)
53        .nest("/mcp", mcp_api)
54        .layer(middleware::from_fn(app_middleware::logging_middleware))
55        .with_state(state)
56}