feat:add server function in RpcServerArgs. (#1402)

Signed-off-by: grapebaba <281165273@qq.com>
Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>
This commit is contained in:
Chen Kai
2023-02-20 08:45:04 +08:00
committed by GitHub
parent eb1299194f
commit e8453abc78
6 changed files with 176 additions and 20 deletions

View File

@ -572,6 +572,21 @@ impl RpcServerConfig {
self
}
/// Returns the [SocketAddr] of the http server
pub fn http_address(&self) -> Option<SocketAddr> {
self.http_addr
}
/// Returns the [SocketAddr] of the ws server
pub fn ws_address(&self) -> Option<SocketAddr> {
self.ws_addr
}
/// Returns the [Endpoint] of the ipc server
pub fn ipc_endpoint(&self) -> Option<&Endpoint> {
self.ipc_endpoint.as_ref()
}
/// Convenience function to do [RpcServerConfig::build] and [RpcServer::start] in one step
pub async fn start(
self,
@ -647,17 +662,17 @@ pub struct TransportRpcModuleConfig {
impl TransportRpcModuleConfig {
/// Creates a new config with only http set
pub fn http(http: impl Into<RpcModuleSelection>) -> Self {
pub fn set_http(http: impl Into<RpcModuleSelection>) -> Self {
Self::default().with_http(http)
}
/// Creates a new config with only ws set
pub fn ws(ws: impl Into<RpcModuleSelection>) -> Self {
pub fn set_ws(ws: impl Into<RpcModuleSelection>) -> Self {
Self::default().with_ws(ws)
}
/// Creates a new config with only ipc set
pub fn ipc(ipc: impl Into<RpcModuleSelection>) -> Self {
pub fn set_ipc(ipc: impl Into<RpcModuleSelection>) -> Self {
Self::default().with_ipc(ipc)
}
@ -683,6 +698,21 @@ impl TransportRpcModuleConfig {
pub fn is_empty(&self) -> bool {
self.http.is_none() && self.ws.is_none() && self.ipc.is_none()
}
/// Returns the [RpcModuleSelection] for the http transport
pub fn http(&self) -> Option<&RpcModuleSelection> {
self.http.as_ref()
}
/// Returns the [RpcModuleSelection] for the ws transport
pub fn ws(&self) -> Option<&RpcModuleSelection> {
self.ws.as_ref()
}
/// Returns the [RpcModuleSelection] for the ipc transport
pub fn ipc(&self) -> Option<&RpcModuleSelection> {
self.ipc.as_ref()
}
}
/// Holds installed modules per transport type.

View File

@ -15,7 +15,7 @@ pub fn test_address() -> SocketAddr {
/// Launches a new server with http only with the given modules
pub async fn launch_http(modules: impl Into<RpcModuleSelection>) -> RpcServerHandle {
let builder = test_rpc_builder();
let server = builder.build(TransportRpcModuleConfig::http(modules));
let server = builder.build(TransportRpcModuleConfig::set_http(modules));
server
.start_server(RpcServerConfig::http(Default::default()).with_http_address(test_address()))
.await
@ -25,7 +25,7 @@ pub async fn launch_http(modules: impl Into<RpcModuleSelection>) -> RpcServerHan
/// Launches a new server with ws only with the given modules
pub async fn launch_ws(modules: impl Into<RpcModuleSelection>) -> RpcServerHandle {
let builder = test_rpc_builder();
let server = builder.build(TransportRpcModuleConfig::ws(modules));
let server = builder.build(TransportRpcModuleConfig::set_ws(modules));
server
.start_server(RpcServerConfig::ws(Default::default()).with_ws_address(test_address()))
.await
@ -36,7 +36,8 @@ pub async fn launch_ws(modules: impl Into<RpcModuleSelection>) -> RpcServerHandl
pub async fn launch_http_ws(modules: impl Into<RpcModuleSelection>) -> RpcServerHandle {
let builder = test_rpc_builder();
let modules = modules.into();
let server = builder.build(TransportRpcModuleConfig::ws(modules.clone()).with_http(modules));
let server =
builder.build(TransportRpcModuleConfig::set_ws(modules.clone()).with_http(modules));
server
.start_server(
RpcServerConfig::ws(Default::default())