Refactor server
This commit is contained in:
parent
0243d23a03
commit
5ddb43351b
2 changed files with 57 additions and 50 deletions
|
@ -1,67 +1,22 @@
|
||||||
#![cfg_attr(not(unix), allow(unused_imports))]
|
#![cfg_attr(not(unix), allow(unused_imports))]
|
||||||
|
|
||||||
|
mod server;
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use tokio::net::UnixListener;
|
use tokio::net::UnixListener;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use tokio_stream::wrappers::UnixListenerStream;
|
use tokio_stream::wrappers::UnixListenerStream;
|
||||||
#[cfg(unix)]
|
|
||||||
use tonic::transport::server::UdsConnectInfo;
|
|
||||||
use tonic::{transport::Server, Request, Response, Status};
|
|
||||||
|
|
||||||
use libcommand::internal::{
|
use tonic::transport::Server;
|
||||||
unix_server::{Unix, UnixServer},
|
use libcommand::internal::unix_server::UnixServer;
|
||||||
AuthorizeRequest, AuthorizeResponse, TerminateRequest, TerminateResponse
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct DaemonServer {}
|
|
||||||
|
|
||||||
#[tonic::async_trait]
|
|
||||||
impl Unix for DaemonServer {
|
|
||||||
async fn authorize(
|
|
||||||
&self,
|
|
||||||
request: Request<AuthorizeRequest>,
|
|
||||||
) -> Result<Response<AuthorizeResponse>, Status> {
|
|
||||||
#[cfg(unix)]
|
|
||||||
{
|
|
||||||
let conn_info = request.extensions().get::<UdsConnectInfo>().unwrap();
|
|
||||||
println!("Got a request {:?} with info {:?}", request, conn_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
let reply = libcommand::internal::AuthorizeResponse {
|
|
||||||
status: libcommand::internal::AuthorizationStatus::Authorized.into(),
|
|
||||||
error_message: "".into(),
|
|
||||||
log_file: "".into(),
|
|
||||||
session_uuid: "".into()
|
|
||||||
};
|
|
||||||
Ok(Response::new(reply))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn terminate(
|
|
||||||
&self,
|
|
||||||
request: Request<TerminateRequest>,
|
|
||||||
) -> Result<Response<TerminateResponse>, Status> {
|
|
||||||
#[cfg(unix)]
|
|
||||||
{
|
|
||||||
let conn_info = request.extensions().get::<UdsConnectInfo>().unwrap();
|
|
||||||
println!("Got a request {:?} with info {:?}", request, conn_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
let reply = libcommand::internal::TerminateResponse {
|
|
||||||
status: libcommand::internal::TerminateStatus::Ok.into(),
|
|
||||||
error_message: "".into(),
|
|
||||||
};
|
|
||||||
Ok(Response::new(reply))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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>> {
|
||||||
std::fs::create_dir_all(Path::new(libcommand::SOCK_FILE).parent().unwrap())?;
|
std::fs::create_dir_all(Path::new(libcommand::SOCK_FILE).parent().unwrap())?;
|
||||||
|
|
||||||
let server = DaemonServer::default();
|
let server = server::DaemonServer::default();
|
||||||
|
|
||||||
let uds = UnixListener::bind(libcommand::SOCK_FILE)?;
|
let uds = UnixListener::bind(libcommand::SOCK_FILE)?;
|
||||||
let uds_stream = UnixListenerStream::new(uds);
|
let uds_stream = UnixListenerStream::new(uds);
|
||||||
|
|
52
src/daemon/server.rs
Normal file
52
src/daemon/server.rs
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
#![cfg_attr(not(unix), allow(unused_imports))]
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
use tonic::transport::server::UdsConnectInfo;
|
||||||
|
use tonic::{transport::Server, Request, Response, Status};
|
||||||
|
|
||||||
|
use libcommand::internal::{
|
||||||
|
unix_server::{Unix, UnixServer},
|
||||||
|
AuthorizeRequest, AuthorizeResponse, TerminateRequest, TerminateResponse
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct DaemonServer {}
|
||||||
|
|
||||||
|
#[tonic::async_trait]
|
||||||
|
impl Unix for DaemonServer {
|
||||||
|
async fn authorize(
|
||||||
|
&self,
|
||||||
|
request: Request<AuthorizeRequest>,
|
||||||
|
) -> Result<Response<AuthorizeResponse>, Status> {
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
let conn_info = request.extensions().get::<UdsConnectInfo>().unwrap();
|
||||||
|
println!("Got a request {:?} with info {:?}", request, conn_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
let reply = libcommand::internal::AuthorizeResponse {
|
||||||
|
status: libcommand::internal::AuthorizationStatus::Authorized.into(),
|
||||||
|
error_message: "".into(),
|
||||||
|
log_file: "".into(),
|
||||||
|
session_uuid: "".into()
|
||||||
|
};
|
||||||
|
Ok(Response::new(reply))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn terminate(
|
||||||
|
&self,
|
||||||
|
request: Request<TerminateRequest>,
|
||||||
|
) -> Result<Response<TerminateResponse>, Status> {
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
let conn_info = request.extensions().get::<UdsConnectInfo>().unwrap();
|
||||||
|
println!("Got a request {:?} with info {:?}", request, conn_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
let reply = libcommand::internal::TerminateResponse {
|
||||||
|
status: libcommand::internal::TerminateStatus::Ok.into(),
|
||||||
|
error_message: "".into(),
|
||||||
|
};
|
||||||
|
Ok(Response::new(reply))
|
||||||
|
}
|
||||||
|
}
|
Reference in a new issue