Skip to main content

spikard_http/jsonrpc/
mod.rs

1//! JSON-RPC protocol support for Spikard HTTP
2//!
3//! This module provides JSON-RPC 2.0 protocol support including method registration,
4//! handler lookup, and metadata management.
5
6pub(crate) mod http_handler;
7pub(crate) mod method_registry;
8pub(crate) mod openrpc;
9pub(crate) mod protocol;
10pub(crate) mod router;
11
12use serde::{Deserialize, Serialize};
13
14#[cfg(not(target_arch = "wasm32"))]
15pub(crate) use http_handler::{JsonRpcState, handle_jsonrpc};
16pub(crate) use method_registry::{JsonRpcMethodRegistry, MethodMetadata};
17#[cfg(not(target_arch = "wasm32"))]
18pub(crate) use openrpc::generate_openrpc_spec;
19#[cfg(not(target_arch = "wasm32"))]
20pub(crate) use router::JsonRpcRouter;
21
22/// JSON-RPC server configuration
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct JsonRpcConfig {
25    /// Enable JSON-RPC endpoint
26    #[serde(default)]
27    pub enabled: bool,
28    /// HTTP endpoint path for JSON-RPC requests (default: "/rpc")
29    #[serde(default = "default_endpoint_path")]
30    pub endpoint_path: String,
31    /// Enable batch request processing (default: true)
32    #[serde(default = "default_true")]
33    pub enable_batch: bool,
34    /// Maximum number of requests in a batch (default: 100)
35    #[serde(default = "default_max_batch_size")]
36    pub max_batch_size: usize,
37}
38
39fn default_endpoint_path() -> String {
40    "/rpc".to_string()
41}
42
43fn default_true() -> bool {
44    true
45}
46
47fn default_max_batch_size() -> usize {
48    100
49}
50
51impl Default for JsonRpcConfig {
52    fn default() -> Self {
53        Self {
54            enabled: true,
55            endpoint_path: default_endpoint_path(),
56            enable_batch: default_true(),
57            max_batch_size: default_max_batch_size(),
58        }
59    }
60}