use crate::config::Scope;
use core::net::SocketAddr;
#[derive(Clone, Debug, Hash, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize)]
pub struct ServerContext {
pub(crate) addr: SocketAddr,
pub(crate) scope: Scope,
}
impl ServerContext {
pub fn new(addr: SocketAddr, scope: Scope) -> Self {
Self { addr, scope }
}
pub fn localhost(port: u16, scope: Scope) -> Self {
Self {
addr: SocketAddr::from(([127, 0, 0, 1], port)),
scope,
}
}
pub fn addr(&self) -> SocketAddr {
self.addr
}
pub const fn scope(&self) -> &Scope {
&self.scope
}
pub fn scope_mut(&mut self) -> &mut Scope {
&mut self.scope
}
setwith! {
addr: SocketAddr,
scope: Scope,
}
pub async fn listen(&self) -> tokio::net::TcpListener {
tokio::net::TcpListener::bind(&self.addr).await.unwrap()
}
}