Files
voidpin/crates/voidpin/src/remote_copy.rs
kjuulh 881094e484
All checks were successful
continuous-integration/drone/push Build is passing
feat: add basic remote copu
2025-01-14 08:16:50 +01:00

51 lines
1.3 KiB
Rust

use tonic::transport::{Channel, ClientTlsConfig};
use crate::{grpc::CopyRequest, state::State};
#[derive(Default)]
pub struct RemoteCopier {
host: String,
}
impl RemoteCopier {
pub fn new(remote_host: impl Into<String>) -> Self {
Self {
host: remote_host.into(),
}
}
pub async fn copy(&self, input: &[u8]) -> anyhow::Result<()> {
let tls = ClientTlsConfig::new();
let channel = Channel::from_shared(self.host.clone())?
.tls_config(if self.host.starts_with("https") {
tls.with_native_roots()
} else {
tls
})?
.connect()
.await?;
tracing::debug!("establishing connection to remote");
let mut client = crate::grpc::void_pin_client::VoidPinClient::new(channel);
tracing::info!("sending copy request");
client
.copy(CopyRequest {
content: input.into(),
})
.await?;
tracing::info!("sent copy request");
Ok(())
}
}
pub trait RemoteCopierState {
fn remote_copier(&self, host: impl Into<String>) -> RemoteCopier;
}
impl RemoteCopierState for State {
fn remote_copier(&self, host: impl Into<String>) -> RemoteCopier {
RemoteCopier::new(host)
}
}