gRPC implemented back

This commit is contained in:
filipriec
2025-02-17 19:19:21 +01:00
parent 03d1c4fff6
commit c150296274
3 changed files with 57 additions and 30 deletions

View File

@@ -6,19 +6,27 @@ use crossterm::{
};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io::{self, stdout};
use tonic::transport::Channel;
use crate::proto::multieko2::adresar_client::AdresarClient;
use crate::proto::multieko2::AdresarRequest;
pub struct AppTerminal {
terminal: Terminal<CrosstermBackend<io::Stdout>>,
grpc_client: AdresarClient<Channel>, // gRPC client
}
impl AppTerminal {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
enable_raw_mode()?; // Moved before execute!
pub async fn new() -> Result<Self, Box<dyn std::error::Error>> {
enable_raw_mode()?;
let mut stdout = stdout();
execute!(stdout, EnterAlternateScreen)?;
let backend = CrosstermBackend::new(stdout);
let terminal = Terminal::new(backend)?;
Ok(Self { terminal })
// Initialize gRPC client
let grpc_client = AdresarClient::connect("http://[::1]:50051").await?;
Ok(Self { terminal, grpc_client })
}
pub fn draw<F>(&mut self, f: F) -> Result<(), Box<dyn std::error::Error>>
@@ -39,37 +47,39 @@ impl AppTerminal {
Ok(())
}
pub fn handle_command(&mut self, command_input: &str, is_saved: &mut bool) -> Result<(bool, String), Box<dyn std::error::Error>> {
pub async fn handle_command(
&mut self,
command_input: &str,
is_saved: &mut bool,
form_data: &AdresarRequest, // Pass form data here
) -> Result<(bool, String), Box<dyn std::error::Error>> {
match command_input {
"w" => {
// Save the state
// Send data to the server
let request = tonic::Request::new(form_data.clone());
let response = self.grpc_client.create_adresar(request).await?;
*is_saved = true;
Ok((false, "State saved.".to_string())) // Do not exit
Ok((false, format!("State saved. Response: {:?}", response)))
}
"q" => {
// Quit if saved
if *is_saved {
self.cleanup()?;
Ok((true, "Exiting.".to_string())) // Exit
Ok((true, "Exiting.".to_string()))
} else {
Ok((false, "No changes saved. Use :q! to force quit.".to_string())) // Do not exit
Ok((false, "No changes saved. Use :q! to force quit.".to_string()))
}
}
"q!" => {
// Force quit without saving
self.cleanup()?;
Ok((true, "Force exiting without saving.".to_string())) // Exit
Ok((true, "Force exiting without saving.".to_string()))
}
"wq" => {
// Save and quit
*is_saved = true;
self.cleanup()?;
Ok((true, "State saved. Exiting.".to_string())) // Exit
}
_ => {
// Handle other commands here
Ok((false, format!("Command not recognized: {}", command_input))) // Do not exit
Ok((true, "State saved. Exiting.".to_string()))
}
_ => Ok((false, format!("Command not recognized: {}", command_input))),
}
}
}