feat(grpc): call walk_dir from grpc service

This commit is contained in:
Alexander Navarro 2024-04-24 11:23:36 -04:00
parent f803aa92f7
commit 8cd4b4b10f
4 changed files with 41 additions and 9 deletions

View file

@ -4,6 +4,7 @@ package juno;
service JunoRequest { service JunoRequest {
rpc Ping (PingRequestMessage) returns (PingResponseMessage); rpc Ping (PingRequestMessage) returns (PingResponseMessage);
rpc GetFiles (GetFilesRequest) returns (GetFilesResponse);
} }
message PingRequestMessage { message PingRequestMessage {
@ -12,3 +13,11 @@ message PingRequestMessage {
message PingResponseMessage { message PingResponseMessage {
string message = 1; string message = 1;
} }
message GetFilesRequest {
string path = 1;
}
message GetFilesResponse {
repeated string files = 1;
}

View file

@ -1,7 +1,7 @@
use super::grpc_juno; use super::grpc_juno;
use grpc_juno::juno_request_client::JunoRequestClient; use grpc_juno::juno_request_client::JunoRequestClient;
use grpc_juno::PingRequestMessage; use grpc_juno::GetFilesRequest;
use tonic::async_trait; use tonic::async_trait;
use tonic::Request; use tonic::Request;
@ -21,11 +21,13 @@ impl super::Connection for GRPCClient {
async fn connect(&self) -> Result<(), Box<dyn std::error::Error>> { async fn connect(&self) -> Result<(), Box<dyn std::error::Error>> {
let mut client = JunoRequestClient::connect(format!("http://{}", self.address)).await?; let mut client = JunoRequestClient::connect(format!("http://{}", self.address)).await?;
let request = Request::new(PingRequestMessage {}); let request = Request::new(GetFilesRequest {
path: "/home/aleidk/Music/".to_string(),
});
let response = client.ping(request).await?; let response = client.get_files(request).await?.into_inner();
println!("RESPONSE={:?}", response); println!("RESPONSE={:?}", response.files);
Ok(()) Ok(())
} }

View file

@ -1,8 +1,12 @@
use crate::file_explorer;
use super::grpc_juno; use super::grpc_juno;
use grpc_juno::juno_request_server::{JunoRequest, JunoRequestServer}; use grpc_juno::juno_request_server::{JunoRequest, JunoRequestServer};
use grpc_juno::{PingRequestMessage, PingResponseMessage}; use grpc_juno::{GetFilesRequest, GetFilesResponse, PingRequestMessage, PingResponseMessage};
use std::error::Error; use std::error::Error;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::path::PathBuf;
use std::str::FromStr;
use tonic::transport::Server; use tonic::transport::Server;
use tonic::{async_trait, Request, Response, Result, Status}; use tonic::{async_trait, Request, Response, Result, Status};
@ -29,6 +33,26 @@ impl JunoRequest for GRPCServer {
Ok(Response::new(reply)) Ok(Response::new(reply))
} }
async fn get_files(
&self,
request: Request<GetFilesRequest>,
) -> Result<Response<GetFilesResponse>, Status> {
let path = PathBuf::from_str(request.into_inner().path.as_str())
.expect("Failed to create pathbuf");
let files = match file_explorer::walk_dir(&path) {
Ok(files) => files,
Err(_err) => panic!("Error reading path: {:?}", path),
};
eprintln!("DEBUGPRINT[2]: server.rs:44: files={:#?}", files);
let reply = GetFilesResponse {
files: files.iter().map(|x| x.display().to_string()).collect(),
};
Ok(Response::new(reply))
}
} }
#[async_trait] #[async_trait]

View file

@ -1,3 +1,4 @@
use core::panic;
use std::{env, path::PathBuf}; use std::{env, path::PathBuf};
use clap::Parser; use clap::Parser;
@ -20,10 +21,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
.path .path
.unwrap_or(env::current_dir().expect("Current directory is not available.")); .unwrap_or(env::current_dir().expect("Current directory is not available."));
let files = file_explorer::walk_dir(&path).expect("error");
eprintln!("DEBUGPRINT[4]: main.rs:20: files={:#?}", files.len());
let server = grpc::run()?; let server = grpc::run()?;
server.connect().await?; server.connect().await?;