Implement gRPC server for DaemonService with container creation dummy method

This commit is contained in:
Noor 2025-01-18 03:38:51 +05:30
parent 17782be7e2
commit e1dbf5c899
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
6 changed files with 1357 additions and 1 deletions

1265
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -4,3 +4,14 @@ version = "0.1.0"
edition = "2021"
[dependencies]
env_logger = "0.11.6"
prost = "0.13.4"
prost-types = "0.13.4"
tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] }
tonic = "0.12.3"
# detee-shared = { git = "ssh://git@gitea.detee.cloud/noormohammedb/detee-shared" }
detee-shared = { path = "../detee-shared" }
[build-dependencies]
tonic-build = "0.12.3"

7
build.rs Normal file

@ -0,0 +1,7 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::configure()
.build_server(true)
.compile_protos(&["daemon.proto"], &["proto"])
.unwrap_or_else(|e| panic!("Failed to compile protos {:?}", e));
Ok(())
}

19
daemon.proto Normal file

@ -0,0 +1,19 @@
syntax = "proto3";
package deamon;
message Empty {
}
message NewContainerReq {
repeated string port = 1;
}
message NewContainerRes {
string status = 1;
}
service DaemonService {
rpc CreateContainer (NewContainerReq) returns (NewContainerRes);
// rpc ListContainer (NodeFilters) returns (stream NodeListResp);
}

46
src/grpc.rs Normal file

@ -0,0 +1,46 @@
use std::{net::SocketAddr, str::FromStr};
use tonic::transport::Server;
use detee_shared::pb::daemon::daemon_service_server::{
DaemonService as DaemonServicePB, DaemonServiceServer as DaemonServiceServerPB,
};
use detee_shared::pb::daemon::NewContainerRes;
use detee_shared::pb::shared::Container;
pub struct DaemonServer {}
impl DaemonServer {
pub fn new() -> Self {
Self {}
}
pub async fn start(&self) -> Result<(), Box<dyn std::error::Error>> {
let port: String = std::env::var("PORT").unwrap_or_else(|_| "33400".to_string());
let data = Self {};
let addr = SocketAddr::from_str(format!("0.0.0.0:{port}").as_str())?;
let daemon_server = DaemonServiceServerPB::new(data);
println!("Listening on {}", addr);
Server::builder()
.add_service(daemon_server)
.serve(addr)
.await?;
Ok(())
}
}
#[tonic::async_trait]
impl DaemonServicePB for DaemonServer {
async fn create_container(
&self,
request: tonic::Request<Container>,
) -> Result<tonic::Response<NewContainerRes>, tonic::Status> {
let req_data = request.into_inner();
dbg!(req_data);
Ok(tonic::Response::new(NewContainerRes::default()))
}
}

@ -1,3 +1,11 @@
fn main() {
mod grpc;
use grpc::DaemonServer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Detee daemon");
DaemonServer::new().start().await?;
Ok(())
}