Refactor client connection
This commit is contained in:
parent
f145bcf383
commit
0243d23a03
4 changed files with 38 additions and 25 deletions
14
src/client/client.rs
Normal file
14
src/client/client.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use libcommand::internal::unix_client::UnixClient;
|
||||
|
||||
#[cfg(unix)]
|
||||
use tokio::net::UnixStream;
|
||||
use tonic::transport::{Channel, Endpoint, Uri};
|
||||
use tower::service_fn;
|
||||
|
||||
pub(super) async fn connect() -> Result<UnixClient<Channel>, Box<dyn std::error::Error>> {
|
||||
let channel = Endpoint::try_from("http://[::]:50051")?
|
||||
.connect_with_connector(service_fn(|_: Uri| UnixStream::connect(libcommand::SOCK_FILE)))
|
||||
.await?;
|
||||
|
||||
Ok(UnixClient::new(channel))
|
||||
}
|
|
@ -1,26 +1,13 @@
|
|||
#![cfg_attr(not(unix), allow(unused_imports))]
|
||||
|
||||
pub mod internal {
|
||||
tonic::include_proto!("internal");
|
||||
}
|
||||
pub mod client;
|
||||
|
||||
use internal::{unix_client::UnixClient, AuthorizeRequest};
|
||||
#[cfg(unix)]
|
||||
use tokio::net::UnixStream;
|
||||
use tonic::transport::{Endpoint, Uri};
|
||||
use tower::service_fn;
|
||||
use libcommand::internal::{AuthorizeRequest};
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let channel = Endpoint::try_from("http://[::]:50051")?
|
||||
.connect_with_connector(service_fn(|_: Uri| {
|
||||
// Connect to a Uds socket
|
||||
UnixStream::connect(libcommand::SOCK_FILE)
|
||||
}))
|
||||
.await?;
|
||||
|
||||
let mut client = UnixClient::new(channel);
|
||||
let mut client = client::connect().await?;
|
||||
|
||||
let request = tonic::Request::new(AuthorizeRequest {
|
||||
identifier: "Tonic".into(),
|
||||
|
|
|
@ -9,11 +9,7 @@ use tokio_stream::wrappers::UnixListenerStream;
|
|||
use tonic::transport::server::UdsConnectInfo;
|
||||
use tonic::{transport::Server, Request, Response, Status};
|
||||
|
||||
pub mod internal {
|
||||
tonic::include_proto!("internal");
|
||||
}
|
||||
|
||||
use internal::{
|
||||
use libcommand::internal::{
|
||||
unix_server::{Unix, UnixServer},
|
||||
AuthorizeRequest, AuthorizeResponse, TerminateRequest, TerminateResponse
|
||||
};
|
||||
|
@ -33,8 +29,8 @@ impl Unix for DaemonServer {
|
|||
println!("Got a request {:?} with info {:?}", request, conn_info);
|
||||
}
|
||||
|
||||
let reply = internal::AuthorizeResponse {
|
||||
status: internal::AuthorizationStatus::Authorized.into(),
|
||||
let reply = libcommand::internal::AuthorizeResponse {
|
||||
status: libcommand::internal::AuthorizationStatus::Authorized.into(),
|
||||
error_message: "".into(),
|
||||
log_file: "".into(),
|
||||
session_uuid: "".into()
|
||||
|
@ -52,8 +48,8 @@ impl Unix for DaemonServer {
|
|||
println!("Got a request {:?} with info {:?}", request, conn_info);
|
||||
}
|
||||
|
||||
let reply = internal::TerminateResponse {
|
||||
status: internal::TerminateStatus::Ok.into(),
|
||||
let reply = libcommand::internal::TerminateResponse {
|
||||
status: libcommand::internal::TerminateStatus::Ok.into(),
|
||||
error_message: "".into(),
|
||||
};
|
||||
Ok(Response::new(reply))
|
||||
|
|
16
src/lib.rs
16
src/lib.rs
|
@ -1,8 +1,24 @@
|
|||
use std::collections::HashMap;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
pub const SOCK_FILE : &'static str = "command_gateway.sock";
|
||||
|
||||
pub mod internal {
|
||||
tonic::include_proto!("internal");
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Command {
|
||||
command: String,
|
||||
envs: HashMap<String, String>,
|
||||
args: Vec<String>
|
||||
}
|
||||
|
||||
impl Into<std::process::Command> for Command {
|
||||
fn into(self) -> std::process::Command {
|
||||
let mut command = std::process::Command::new(self.command);
|
||||
command.args(self.args);
|
||||
command.envs(self.envs);
|
||||
command
|
||||
}
|
||||
}
|
Reference in a new issue