Merge pull request #1 from aleee-idk/feature/client-server
Feature/client server
This commit is contained in:
commit
ad61cf68c6
9 changed files with 250 additions and 22 deletions
|
|
@ -6,3 +6,10 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.4", features = ["derive"] }
|
clap = { version = "4.5.4", features = ["derive"] }
|
||||||
ignore = "0.4.22"
|
ignore = "0.4.22"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
|
prost = "0.12.4"
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
tonic = "0.11.0"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
tonic-build = "0.11.0"
|
||||||
|
|
|
||||||
5
build.rs
Normal file
5
build.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
tonic_build::compile_protos("proto/juno.proto")?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
23
proto/juno.proto
Normal file
23
proto/juno.proto
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package juno;
|
||||||
|
|
||||||
|
service JunoRequest {
|
||||||
|
rpc Ping (PingRequestMessage) returns (PingResponseMessage);
|
||||||
|
rpc GetFiles (GetFilesRequest) returns (GetFilesResponse);
|
||||||
|
}
|
||||||
|
|
||||||
|
message PingRequestMessage {
|
||||||
|
}
|
||||||
|
|
||||||
|
message PingResponseMessage {
|
||||||
|
string message = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetFilesRequest {
|
||||||
|
string path = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
message GetFilesResponse {
|
||||||
|
repeated string files = 1;
|
||||||
|
}
|
||||||
46
src/configuration.rs
Normal file
46
src/configuration.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
use clap::Parser;
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use std::env;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref CONFIG: Config = Config::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(version, about, long_about = None)]
|
||||||
|
struct Args {
|
||||||
|
#[arg(help = "Directory to scan for files")]
|
||||||
|
path: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Config {
|
||||||
|
pub base_path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Config {
|
||||||
|
fn default() -> Self {
|
||||||
|
Config {
|
||||||
|
base_path: env::current_dir().expect("Current directory is not available."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let mut config = Self::default();
|
||||||
|
|
||||||
|
let cli = Self::get_cli_args();
|
||||||
|
|
||||||
|
if let Some(path) = cli.path {
|
||||||
|
config.base_path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
config
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_cli_args() -> Args {
|
||||||
|
Args::parse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
use ignore::types::TypesBuilder;
|
use ignore::types::TypesBuilder;
|
||||||
use ignore::WalkBuilder;
|
use ignore::WalkBuilder;
|
||||||
use std::{io, path::PathBuf};
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub fn walk_dir(path: &PathBuf) -> io::Result<Vec<PathBuf>> {
|
use crate::configuration::CONFIG;
|
||||||
|
|
||||||
|
pub fn walk_dir(path: &PathBuf) -> Result<Vec<PathBuf>, &str> {
|
||||||
let mut types_builder = TypesBuilder::new();
|
let mut types_builder = TypesBuilder::new();
|
||||||
types_builder.add_defaults();
|
types_builder.add_defaults();
|
||||||
|
|
||||||
|
|
@ -14,7 +16,19 @@ pub fn walk_dir(path: &PathBuf) -> io::Result<Vec<PathBuf>> {
|
||||||
|
|
||||||
types_builder.select("sound");
|
types_builder.select("sound");
|
||||||
|
|
||||||
let entries: Vec<PathBuf> = WalkBuilder::new(path)
|
let search_path = CONFIG.base_path.join(path);
|
||||||
|
eprintln!(
|
||||||
|
"DEBUGPRINT[1]: file_explorer.rs:19: search_path={:#?}",
|
||||||
|
search_path
|
||||||
|
);
|
||||||
|
|
||||||
|
// PathBuf.join() can override the hole path, this ensure we're not accessing files outside
|
||||||
|
// base_dir
|
||||||
|
if !search_path.starts_with(&CONFIG.base_path) {
|
||||||
|
return Err("Tried to access file or directory outside of server `base_dir` config.");
|
||||||
|
}
|
||||||
|
|
||||||
|
let entries: Vec<PathBuf> = WalkBuilder::new(search_path)
|
||||||
.types(types_builder.build().unwrap())
|
.types(types_builder.build().unwrap())
|
||||||
.build()
|
.build()
|
||||||
.filter_map(|entry| entry.ok())
|
.filter_map(|entry| entry.ok())
|
||||||
|
|
|
||||||
37
src/grpc.rs
Normal file
37
src/grpc.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
use std::error::Error;
|
||||||
|
use std::net::{SocketAddr, TcpListener};
|
||||||
|
|
||||||
|
use tonic::async_trait;
|
||||||
|
|
||||||
|
use self::client::GRPCClient;
|
||||||
|
use self::server::GRPCServer;
|
||||||
|
|
||||||
|
mod client;
|
||||||
|
mod server;
|
||||||
|
|
||||||
|
pub mod grpc_juno {
|
||||||
|
tonic::include_proto!("juno");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait Connection {
|
||||||
|
async fn connect(&self) -> Result<(), Box<dyn Error>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_socket_in_use(addr: String) -> bool {
|
||||||
|
let socket: SocketAddr = addr.parse().expect("Failed to create socket");
|
||||||
|
match TcpListener::bind(socket) {
|
||||||
|
Ok(_) => true,
|
||||||
|
Err(_) => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run() -> Result<Box<dyn Connection>, Box<dyn Error>> {
|
||||||
|
let addr = "[::1]:50051";
|
||||||
|
|
||||||
|
if is_socket_in_use(addr.to_string()) {
|
||||||
|
Ok(Box::new(GRPCServer::new(addr.to_string())))
|
||||||
|
} else {
|
||||||
|
Ok(Box::new(GRPCClient::new(addr.to_string())))
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/grpc/client.rs
Normal file
34
src/grpc/client.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
use super::grpc_juno;
|
||||||
|
|
||||||
|
use grpc_juno::juno_request_client::JunoRequestClient;
|
||||||
|
use grpc_juno::GetFilesRequest;
|
||||||
|
use tonic::async_trait;
|
||||||
|
use tonic::Request;
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct GRPCClient {
|
||||||
|
address: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GRPCClient {
|
||||||
|
pub fn new(address: String) -> Self {
|
||||||
|
Self { address }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl super::Connection for GRPCClient {
|
||||||
|
async fn connect(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let mut client = JunoRequestClient::connect(format!("http://{}", self.address)).await?;
|
||||||
|
|
||||||
|
let request = Request::new(GetFilesRequest {
|
||||||
|
path: "/home/aleidk/Documents/".to_string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
let response = client.get_files(request).await?.into_inner();
|
||||||
|
|
||||||
|
println!("RESPONSE={:?}", response.files);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
71
src/grpc/server.rs
Normal file
71
src/grpc/server.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
use crate::file_explorer;
|
||||||
|
|
||||||
|
use super::grpc_juno;
|
||||||
|
use grpc_juno::juno_request_server::{JunoRequest, JunoRequestServer};
|
||||||
|
use grpc_juno::{GetFilesRequest, GetFilesResponse, PingRequestMessage, PingResponseMessage};
|
||||||
|
use std::error::Error;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::str::FromStr;
|
||||||
|
use tonic::transport::Server;
|
||||||
|
use tonic::{async_trait, Request, Response, Result, Status};
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct GRPCServer {
|
||||||
|
address: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GRPCServer {
|
||||||
|
pub fn new(address: String) -> Self {
|
||||||
|
Self { address }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tonic::async_trait]
|
||||||
|
impl JunoRequest for GRPCServer {
|
||||||
|
async fn ping(
|
||||||
|
&self,
|
||||||
|
_request: Request<PingRequestMessage>,
|
||||||
|
) -> Result<Response<PingResponseMessage>, Status> {
|
||||||
|
let reply = PingResponseMessage {
|
||||||
|
message: "pong!".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
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) => return Err(Status::invalid_argument(err)),
|
||||||
|
};
|
||||||
|
|
||||||
|
let reply = GetFilesResponse {
|
||||||
|
files: files.iter().map(|x| x.display().to_string()).collect(),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Response::new(reply))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl super::Connection for GRPCServer {
|
||||||
|
async fn connect(&self) -> Result<(), Box<dyn Error>> {
|
||||||
|
println!("Starting server on: \"{}\"", self.address);
|
||||||
|
|
||||||
|
let socket: SocketAddr = self.address.parse()?;
|
||||||
|
|
||||||
|
Server::builder()
|
||||||
|
.add_service(JunoRequestServer::new(GRPCServer::default()))
|
||||||
|
.serve(socket)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/main.rs
29
src/main.rs
|
|
@ -1,23 +1,14 @@
|
||||||
use std::{env, path::PathBuf};
|
use std::error::Error;
|
||||||
|
|
||||||
use clap::Parser;
|
|
||||||
|
|
||||||
|
mod configuration;
|
||||||
mod file_explorer;
|
mod file_explorer;
|
||||||
|
mod grpc;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[tokio::main()]
|
||||||
#[command(version, about, long_about = None)]
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
struct Args {
|
let server = grpc::run()?;
|
||||||
#[arg(help = "Directory to scan for files")]
|
|
||||||
path: Option<PathBuf>,
|
server.connect().await?;
|
||||||
}
|
|
||||||
|
Ok(())
|
||||||
fn main() {
|
|
||||||
let cli = Args::parse();
|
|
||||||
let path = cli
|
|
||||||
.path
|
|
||||||
.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);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue