refactor: remove struct
CI / test (push) Has been cancelled
CI / packaging (push) Has been cancelled
CI / format (push) Has been cancelled

This commit is contained in:
Tien Do Nam
2025-11-19 22:23:21 +01:00
parent 5ac1e66026
commit e7097299b3
2 changed files with 37 additions and 54 deletions
+29 -52
View File
@@ -56,63 +56,40 @@ impl AppState {
}
}
pub struct LsHttpServer {
state: AppState,
stop_tx: Arc<Mutex<Option<oneshot::Sender<()>>>>,
}
/// Binds the server to the specified port on both IPv4 and IPv6 addresses.
pub async fn start_with_port(
port: u16,
tls_config: Option<TlsConfig>,
info: ClientInfo,
legacy_enabled: bool,
stop_rx: oneshot::Receiver<()>,
) -> anyhow::Result<()> {
let ipv4_socket_addr = SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), port);
let ipv6_socket_addr = SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), port);
let info = Arc::new(Mutex::new(info));
let state = AppState::new(info.clone());
impl LsHttpServer {
/// Binds the server to the specified port on both IPv4 and IPv6 addresses.
pub async fn start_with_port(
port: u16,
tls_config: Option<TlsConfig>,
info: ClientInfo,
legacy_enabled: bool,
) -> anyhow::Result<LsHttpServer> {
let ipv4_socket_addr = SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), port);
let ipv6_socket_addr = SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), port);
let info = Arc::new(Mutex::new(info));
let state = AppState::new(info.clone());
let (stop_tx, stop_rx) = oneshot::channel::<()>();
tokio::spawn({
let state = state.clone();
async move {
tokio::select! {
_ = start_server_with_addr(ipv4_socket_addr, tls_config.clone(), state.clone(), legacy_enabled) => {
tracing::info!("Server stopped on: {}", ipv4_socket_addr);
}
_ = async {
if start_server_with_addr(ipv6_socket_addr, tls_config, state, legacy_enabled).await.is_err() {
tracing::warn!("Failed to start server on: {}", ipv6_socket_addr);
// Keep the future running forever, so we continue using "ipv4 only" even if ipv6 fails.
tokio::time::sleep(std::time::Duration::from_secs(u64::MAX)).await;
}
} => {}
_ = stop_rx => {}
tokio::spawn({
let state = state.clone();
async move {
tokio::select! {
_ = start_server_with_addr(ipv4_socket_addr, tls_config.clone(), state.clone(), legacy_enabled) => {
tracing::info!("Server stopped on: {}", ipv4_socket_addr);
}
_ = async {
if start_server_with_addr(ipv6_socket_addr, tls_config, state, legacy_enabled).await.is_err() {
tracing::warn!("Failed to start server on: {}", ipv6_socket_addr);
// Keep the future running forever, so we continue using "ipv4 only" even if ipv6 fails.
tokio::time::sleep(std::time::Duration::from_secs(u64::MAX)).await;
}
} => {}
_ = stop_rx => {}
}
});
Ok(LsHttpServer {
state,
stop_tx: Arc::new(Mutex::new(Some(stop_tx))),
})
}
pub async fn stop(&self) -> anyhow::Result<()> {
let mut stop_tx = self.stop_tx.lock().await;
if let Some(stop_tx) = stop_tx.take() {
stop_tx
.send(())
.map_err(|_| anyhow::anyhow!("Failed to send stop signal"))?;
}
});
Ok(())
}
Ok(())
}
#[derive(Clone, Debug)]
+8 -2
View File
@@ -134,7 +134,10 @@ async fn server_test() -> Result<()> {
device_type: None,
token: "456".to_string(),
};
let server = http::server::LsHttpServer::start_with_port(
let (stop_tx, stop_rx) = oneshot::channel::<()>();
http::server::start_with_port(
53317,
Some(TlsConfig {
cert: CERT.to_string(),
@@ -142,10 +145,13 @@ async fn server_test() -> Result<()> {
}),
client_info,
true,
stop_rx,
)
.await?;
tokio::time::sleep(std::time::Duration::from_secs(u64::MAX)).await;
server.stop().await?;
let _ = stop_tx.send(());
Ok(())
}