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))]
|
#![cfg_attr(not(unix), allow(unused_imports))]
|
||||||
|
|
||||||
pub mod internal {
|
pub mod client;
|
||||||
tonic::include_proto!("internal");
|
|
||||||
}
|
|
||||||
|
|
||||||
use internal::{unix_client::UnixClient, AuthorizeRequest};
|
use libcommand::internal::{AuthorizeRequest};
|
||||||
#[cfg(unix)]
|
|
||||||
use tokio::net::UnixStream;
|
|
||||||
use tonic::transport::{Endpoint, Uri};
|
|
||||||
use tower::service_fn;
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let channel = Endpoint::try_from("http://[::]:50051")?
|
let mut client = client::connect().await?;
|
||||||
.connect_with_connector(service_fn(|_: Uri| {
|
|
||||||
// Connect to a Uds socket
|
|
||||||
UnixStream::connect(libcommand::SOCK_FILE)
|
|
||||||
}))
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
let mut client = UnixClient::new(channel);
|
|
||||||
|
|
||||||
let request = tonic::Request::new(AuthorizeRequest {
|
let request = tonic::Request::new(AuthorizeRequest {
|
||||||
identifier: "Tonic".into(),
|
identifier: "Tonic".into(),
|
||||||
|
|
|
@ -9,11 +9,7 @@ use tokio_stream::wrappers::UnixListenerStream;
|
||||||
use tonic::transport::server::UdsConnectInfo;
|
use tonic::transport::server::UdsConnectInfo;
|
||||||
use tonic::{transport::Server, Request, Response, Status};
|
use tonic::{transport::Server, Request, Response, Status};
|
||||||
|
|
||||||
pub mod internal {
|
use libcommand::internal::{
|
||||||
tonic::include_proto!("internal");
|
|
||||||
}
|
|
||||||
|
|
||||||
use internal::{
|
|
||||||
unix_server::{Unix, UnixServer},
|
unix_server::{Unix, UnixServer},
|
||||||
AuthorizeRequest, AuthorizeResponse, TerminateRequest, TerminateResponse
|
AuthorizeRequest, AuthorizeResponse, TerminateRequest, TerminateResponse
|
||||||
};
|
};
|
||||||
|
@ -33,8 +29,8 @@ impl Unix for DaemonServer {
|
||||||
println!("Got a request {:?} with info {:?}", request, conn_info);
|
println!("Got a request {:?} with info {:?}", request, conn_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
let reply = internal::AuthorizeResponse {
|
let reply = libcommand::internal::AuthorizeResponse {
|
||||||
status: internal::AuthorizationStatus::Authorized.into(),
|
status: libcommand::internal::AuthorizationStatus::Authorized.into(),
|
||||||
error_message: "".into(),
|
error_message: "".into(),
|
||||||
log_file: "".into(),
|
log_file: "".into(),
|
||||||
session_uuid: "".into()
|
session_uuid: "".into()
|
||||||
|
@ -52,8 +48,8 @@ impl Unix for DaemonServer {
|
||||||
println!("Got a request {:?} with info {:?}", request, conn_info);
|
println!("Got a request {:?} with info {:?}", request, conn_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
let reply = internal::TerminateResponse {
|
let reply = libcommand::internal::TerminateResponse {
|
||||||
status: internal::TerminateStatus::Ok.into(),
|
status: libcommand::internal::TerminateStatus::Ok.into(),
|
||||||
error_message: "".into(),
|
error_message: "".into(),
|
||||||
};
|
};
|
||||||
Ok(Response::new(reply))
|
Ok(Response::new(reply))
|
||||||
|
|
16
src/lib.rs
16
src/lib.rs
|
@ -1,8 +1,24 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
pub const SOCK_FILE : &'static str = "command_gateway.sock";
|
pub const SOCK_FILE : &'static str = "command_gateway.sock";
|
||||||
|
|
||||||
|
pub mod internal {
|
||||||
|
tonic::include_proto!("internal");
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct Command {
|
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