use jsonrpc_http_server::{AccessControlAllowOrigin, DomainsValidation};
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
#[derive(Debug, PartialEq)]
pub struct TcpConfiguration {
pub enabled: bool,
pub address: SocketAddr,
}
impl TcpConfiguration {
pub fn new(ip: Option<(u8, u8, u8, u8)>, port: Option<u16>) -> Self {
let ipv4 = match ip {
Some(ip) => Ipv4Addr::new(ip.0, ip.1, ip.2, ip.3),
None => Ipv4Addr::new(0, 0, 0, 0),
};
TcpConfiguration {
enabled: port.is_some(),
address: SocketAddr::V4(SocketAddrV4::new(ipv4, port.unwrap_or(0))),
}
}
}
#[derive(Debug, PartialEq)]
pub struct HttpConfiguration {
pub enabled: bool,
pub address: SocketAddr,
pub cors_domains: DomainsValidation<AccessControlAllowOrigin>,
pub keep_alive: bool,
pub threads: Option<usize>,
}
impl HttpConfiguration {
pub fn new(
ip: Option<(u8, u8, u8, u8)>, port: Option<u16>, cors: Option<String>,
keep_alive: bool, threads: Option<usize>,
) -> Self {
let ipv4 = match ip {
Some(ip) => Ipv4Addr::new(ip.0, ip.1, ip.2, ip.3),
None => Ipv4Addr::new(0, 0, 0, 0),
};
HttpConfiguration {
enabled: port.is_some(),
address: SocketAddr::V4(SocketAddrV4::new(ipv4, port.unwrap_or(0))),
cors_domains: match cors {
None => DomainsValidation::Disabled,
Some(cors_list) => match cors_list.as_str() {
"none" => DomainsValidation::Disabled,
"all" => DomainsValidation::AllowOnly(vec![
AccessControlAllowOrigin::Any,
]),
_ => DomainsValidation::AllowOnly(
cors_list.split(',').map(Into::into).collect(),
),
},
},
keep_alive,
threads,
}
}
}
#[derive(Debug, PartialEq)]
pub struct WsConfiguration {
pub enabled: bool,
pub address: SocketAddr,
pub max_payload_bytes: usize,
}
impl WsConfiguration {
pub fn new(
ip: Option<(u8, u8, u8, u8)>, port: Option<u16>,
max_payload_bytes: usize,
) -> Self {
let ipv4 = match ip {
Some(ip) => Ipv4Addr::new(ip.0, ip.1, ip.2, ip.3),
None => Ipv4Addr::new(0, 0, 0, 0),
};
WsConfiguration {
enabled: port.is_some(),
address: SocketAddr::V4(SocketAddrV4::new(ipv4, port.unwrap_or(0))),
max_payload_bytes,
}
}
}