50 lines
1.6 KiB
Rust
50 lines
1.6 KiB
Rust
use std::net::SocketAddr;
|
|
use std::path::PathBuf;
|
|
use jsonrpsee::server::{ServerBuilder, ServerHandle};
|
|
use jsonrpsee::RpcModule;
|
|
|
|
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)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use std::time::Duration;
|
|
|
|
#[tokio::test]
|
|
async fn test_rpc_server_startup() {
|
|
let addr = "127.0.0.1:0".parse().unwrap(); // Use port 0 for auto-assignment
|
|
let base_dir = PathBuf::from("/tmp/test_rpc");
|
|
let backend = crate::options::BackendType::Redb; // Default for test
|
|
|
|
let handle = start_rpc_server(addr, base_dir, backend, "test-admin".to_string()).await.unwrap();
|
|
|
|
// Give the server a moment to start
|
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
|
|
|
// Stop the server
|
|
handle.stop().unwrap();
|
|
handle.stopped().await;
|
|
}
|
|
} |