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 mod http_handler;
7pub mod method_registry;
8pub mod openrpc;
9pub mod protocol;
10pub mod router;
11
12use serde::{Deserialize, Serialize};
13
14pub use http_handler::{JsonRpcState, handle_jsonrpc};
15pub use method_registry::{JsonRpcMethodRegistry, MethodExample, MethodMetadata};
16pub use openrpc::generate_openrpc_spec;
17pub use protocol::{
18    JsonRpcErrorResponse, JsonRpcRequest, JsonRpcResponse, JsonRpcResponseType, error_codes, validate_method_name,
19};
20pub use router::{JsonRpcRequestOrBatch, 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}