From f397ad858fff66cbee5a49e245aa7675accd22dc Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 7 Nov 2021 16:40:13 +0100 Subject: [PATCH] Change connection implementation to use the .tcp file in the repository Signed-off-by: trivernis --- .../src/tauri_plugin/commands/repo.rs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index fd39b4d..86fbb6c 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -7,6 +7,7 @@ use std::mem; use std::path::PathBuf; use std::time::{SystemTime, UNIX_EPOCH}; use tokio::fs; +use tokio::time::Duration; static REPO_CONFIG_FILE: &str = "repo.toml"; @@ -134,14 +135,21 @@ pub async fn select_repository( let address = if let Some(address) = &repo.address { address.clone() } else { - tracing::debug!("Reading repo address from config."); + tracing::debug!("Reading repo address from local file."); let path = repo .path .clone() .ok_or_else(|| PluginError::from("Missing repo path or address in config."))?; - let config = read_repo_config(PathBuf::from(path).join(REPO_CONFIG_FILE)).await?; - - config.listen_address + let address_path = PathBuf::from(path).join(".tcp"); + let mut address = String::from("127.0.0.1:2400"); + for _ in 0..10 { + if address_path.exists() { + address = fs::read_to_string(address_path).await?; + break; + } + tokio::time::sleep(Duration::from_millis(250)).await; + } + address }; let client = ApiClient::connect(address).await?; api_state.set_api(client).await; @@ -173,10 +181,3 @@ async fn close_selected_repository(app_state: &AppAccess<'_>) -> PluginResult<() Ok(()) } - -async fn read_repo_config(path: PathBuf) -> PluginResult { - let toml_str = fs::read_to_string(path).await?; - let config = toml::from_str(&toml_str)?; - - Ok(config) -}