Skip to main content

routa_server/api/
rpc.rs

1//! JSON-RPC 2.0 endpoint powered by `crate::rpc`.
2//!
3//! Exposes `POST /api/rpc` — a single endpoint for all JSON-RPC method calls.
4//! Also exposes `GET /api/rpc/methods` for method discovery.
5
6use axum::{
7    extract::State,
8    routing::{get, post},
9    Json, Router,
10};
11
12use crate::rpc::RpcRouter;
13use crate::state::AppState;
14
15pub fn router() -> Router<AppState> {
16    Router::new()
17        .route("/", post(rpc_handler))
18        .route("/methods", get(list_methods))
19}
20
21/// POST /api/rpc — JSON-RPC 2.0 endpoint.
22///
23/// Accepts a JSON-RPC request (single or batch) and returns the response.
24async fn rpc_handler(
25    State(state): State<AppState>,
26    Json(body): Json<serde_json::Value>,
27) -> Json<serde_json::Value> {
28    let rpc = RpcRouter::new(state);
29    let response = rpc.handle_value(body).await;
30    Json(response)
31}
32
33/// GET /api/rpc/methods — list all supported JSON-RPC method names.
34async fn list_methods(State(state): State<AppState>) -> Json<serde_json::Value> {
35    let rpc = RpcRouter::new(state);
36    let methods = rpc.method_list();
37    Json(serde_json::json!({ "methods": methods }))
38}