diff --git a/mediarepo-daemon/mediarepo-socket/src/lib.rs b/mediarepo-daemon/mediarepo-socket/src/lib.rs index aa49f57..75cc96b 100644 --- a/mediarepo-daemon/mediarepo-socket/src/lib.rs +++ b/mediarepo-daemon/mediarepo-socket/src/lib.rs @@ -7,13 +7,14 @@ use mediarepo_model::repo::Repo; use mediarepo_model::type_keys::RepoKey; use std::net::{IpAddr, SocketAddr}; use std::sync::Arc; -use tokio::net::TcpListener; +use tokio::net::{TcpListener, UnixListener}; use tokio::task::JoinHandle; mod from_model; mod namespaces; mod utils; +#[tracing::instrument(skip(settings, repo))] pub fn start_tcp_server( ip: IpAddr, port_range: (u16, u16), @@ -37,6 +38,30 @@ pub fn start_tcp_server( Ok((address_string, join_handle)) } +#[cfg(unix)] +#[tracing::instrument(skip(settings, repo))] +pub fn create_unix_socket( + path: std::path::PathBuf, + settings: Settings, + repo: Repo, +) -> RepoResult> { + use std::fs; + + if path.exists() { + fs::remove_file(&path)?; + } + let join_handle = tokio::task::spawn(async move { + get_builder::(path) + .insert::(Arc::new(repo)) + .insert::(settings) + .build_server() + .await + .expect("Failed to create unix domain socket"); + }); + + Ok(join_handle) +} + fn get_builder(address: L::AddressType) -> IPCBuilder { namespaces::build_namespaces(IPCBuilder::new().address(address)).on("info", callback!(info)) } diff --git a/mediarepo-daemon/src/main.rs b/mediarepo-daemon/src/main.rs index f388332..d91a3ce 100644 --- a/mediarepo-daemon/src/main.rs +++ b/mediarepo-daemon/src/main.rs @@ -121,14 +121,25 @@ async fn init_repo(opt: &Opt) -> RepoResult<(Settings, Repo)> { /// Starts the server async fn start_server(opt: Opt) -> RepoResult<()> { let (settings, repo) = init_repo(&opt).await?; - let (address, handle) = start_tcp_server( + let mut handles = Vec::new(); + + #[cfg(unix)] + { + let socket_path = opt.repo.join("repo.sock"); + let handle = + mediarepo_socket::create_unix_socket(socket_path, settings.clone(), repo.clone())?; + handles.push(handle); + } + + let (address, tcp_handle) = start_tcp_server( settings.listen_address.clone(), settings.port_range, settings, repo, )?; - fs::write(opt.repo.join(".tcp"), &address.into_bytes()).await?; - handle.await.unwrap(); + handles.push(tcp_handle); + fs::write(opt.repo.join("repo.tcp"), &address.into_bytes()).await?; + futures::future::join_all(handles.into_iter()).await; Ok(()) }