Files
herodb/src/rpc_server.rs
2025-10-21 16:37:11 +02:00

52 lines
1.7 KiB
Rust

use std::net::SocketAddr;
use std::path::PathBuf;
use jsonrpsee::server::{ServerBuilder, ServerHandle};
use jsonrpsee::RpcModule;
use reth_ipc::server::Builder as IpcServerBuilder;
use crate::rpc::{RpcServer, RpcServerImpl};
/// Start the RPC server on the specified address
pub async fn start_rpc_server(addr: SocketAddr, base_dir: PathBuf, backend: crate::options::BackendType, admin_secret: String) -> Result<ServerHandle, Box<dyn std::error::Error + Send + Sync>> {
// Create the RPC server implementation
let rpc_impl = RpcServerImpl::new(base_dir, backend, admin_secret);
// Create the RPC module
let mut module = RpcModule::new(());
module.merge(RpcServer::into_rpc(rpc_impl))?;
// Build the server with both HTTP and WebSocket support
let server = ServerBuilder::default()
.build(addr)
.await?;
// Start the server
let handle = server.start(module);
println!("RPC server started on {}", addr);
Ok(handle)
}
/// Start the JSON-RPC IPC server on the specified Unix socket endpoint
pub async fn start_rpc_ipc_server(
endpoint: String,
base_dir: PathBuf,
backend: crate::options::BackendType,
admin_secret: String,
) -> Result<ServerHandle, Box<dyn std::error::Error + Send + Sync>> {
// Create the RPC server implementation
let rpc_impl = RpcServerImpl::new(base_dir, backend, admin_secret);
// Create the RPC module
let mut module = RpcModule::new(());
module.merge(RpcServer::into_rpc(rpc_impl))?;
// Build the IPC server and start it
let server = IpcServerBuilder::default().build(endpoint.clone());
let handle = server.start(module).await?;
println!("RPC IPC server started on {}", endpoint);
Ok(handle)
}