pull_local_config + init_config_grub_mechanism (50%) + init_unix_listener

pull/3/head
prplV 2025-01-16 14:38:22 +03:00
parent e777571d12
commit 4c88b27d82
1 changed files with 61 additions and 4 deletions

View File

@ -2,13 +2,70 @@
// 1) check changes in unix-socket
// 2) save changes in local config file
use integr_structs::api::ApiConfig;
use tokio::net::UnixListener;
use anyhow::Result;
use anyhow::{Error, Ok, Result};
use log::{info, warn};
use std::{fs, path::Path};
use serde_json::from_str;
use tokio::{io::AsyncReadExt, net::UnixListener};
use std::io;
use tokio::time::{sleep, Duration};
use std::result::Result::Ok as stdOk;
// todo! rewrite to use current_exe
pub async fn pull_local_config() -> Result<ApiConfig> {
let conf_path = std::env::current_exe()?;
let path = Path::new("config_api.json");
// return match conf_path.parent() {
// Some(dir) => {
// let config: ApiConfig = from_str(
// &fs::read_to_string(dir.join("config_api.json"))?
// )?;
// Ok(config)
// },
// None => Err(Error::msg("No local conf was found"))
// }
if path.exists() && path.is_file() {
let config: ApiConfig = from_str(
&fs::read_to_string("config_api.json")?
)?;
Ok(config)
} else {
Err(Error::msg("No local conf was found"))
}
}
// for config pulling
async fn init_api_grub_mechanism(config: ApiConfig) {
// ++++ reader to channel
async fn init_config_grub_mechanism(config: ApiConfig) -> Result<()> {
let server = init_unix_listener().await?;
//
info!("Listening Unix-Socket...");
let mut buffer = String::new();
//
loop {
if let stdOk((mut stream, _)) = server.accept().await {
if let Err(er) = stream.read_to_string(&mut buffer).await {
warn!("Cannot read config from stream due to {}", er);
sleep(Duration::from_millis(500)).await;
} else {
let config: Result<ApiConfig, serde_json::Error> = from_str(&buffer);
if let stdOk(conf) = config {
} else {
}
}
}
}
//
Ok(())
}
async fn save_new_config() -> Result<()> {
Ok(())
}
async fn init_unix_listener() {}
async fn init_unix_listener() -> Result<UnixListener> {
fs::remove_file("api-grub.sock")?;
Ok(UnixListener::bind("api-grub.sock")?)
}