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    // Combine all routes with logging middleware
40    Router::new()
41        .nest("/api/v1", rest_api)
42        .nest("/api/v1/rpc", rpc_api)
43        .layer(middleware::from_fn(app_middleware::logging_middleware))
44        .with_state(state)
45}