Implement support for unix domain sockets on unix

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 9b2092270a
commit ce2533292d

@ -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<JoinHandle<()>> {
use std::fs;
if path.exists() {
fs::remove_file(&path)?;
}
let join_handle = tokio::task::spawn(async move {
get_builder::<UnixListener>(path)
.insert::<RepoKey>(Arc::new(repo))
.insert::<SettingsKey>(settings)
.build_server()
.await
.expect("Failed to create unix domain socket");
});
Ok(join_handle)
}
fn get_builder<L: AsyncStreamProtocolListener>(address: L::AddressType) -> IPCBuilder<L> {
namespaces::build_namespaces(IPCBuilder::new().address(address)).on("info", callback!(info))
}

@ -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(())
}

Loading…
Cancel
Save