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 protocol;
9pub mod router;
10
11use serde::{Deserialize, Serialize};
12
13pub use http_handler::{JsonRpcState, handle_jsonrpc};
14pub use method_registry::{JsonRpcMethodRegistry, MethodExample, MethodMetadata};
15pub use protocol::{
16    JsonRpcErrorResponse, JsonRpcRequest, JsonRpcResponse, JsonRpcResponseType, error_codes, validate_method_name,
17};
18pub use router::{JsonRpcRequestOrBatch, JsonRpcRouter};
19
20/// JSON-RPC server configuration
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct JsonRpcConfig {
23    /// Enable JSON-RPC endpoint
24    #[serde(default)]
25    pub enabled: bool,
26    /// HTTP endpoint path for JSON-RPC requests (default: "/rpc")
27    #[serde(default = "default_endpoint_path")]
28    pub endpoint_path: String,
29    /// Enable batch request processing (default: true)
30    #[serde(default = "default_true")]
31    pub enable_batch: bool,
32    /// Maximum number of requests in a batch (default: 100)
33    #[serde(default = "default_max_batch_size")]
34    pub max_batch_size: usize,
35}
36
37fn default_endpoint_path() -> String {
38    "/rpc".to_string()
39}
40
41fn default_true() -> bool {
42    true
43}
44
45fn default_max_batch_size() -> usize {
46    100
47}
48
49impl Default for JsonRpcConfig {
50    fn default() -> Self {
51        Self {
52            enabled: true,
53            endpoint_path: default_endpoint_path(),
54            enable_batch: default_true(),
55            max_batch_size: default_max_batch_size(),
56        }
57    }
58}