unixsocket fix + disable lint warnings

pull/3/head
prplV 2025-01-16 15:12:35 +03:00
parent 4c88b27d82
commit 991bae50f6
4 changed files with 46 additions and 22 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "api-grub" name = "api-grub"
version = "0.1.0" version = "0.3.8"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -3,22 +3,24 @@
// 2) save changes in local config file // 2) save changes in local config file
use integr_structs::api::ApiConfig; use integr_structs::api::ApiConfig;
use anyhow::{Error, Ok, Result}; use anyhow::{Error, Ok, Result};
use log::{info, warn}; use log::{info, warn, error};
use std::{fs, path::Path}; use std::{fs, path::Path};
use serde_json::from_str; use serde_json::from_str;
use tokio::{io::AsyncReadExt, net::UnixListener}; use tokio::{io::AsyncReadExt, net::UnixListener};
use std::io;
use tokio::time::{sleep, Duration}; use tokio::time::{sleep, Duration};
use std::result::Result::Ok as stdOk; use std::result::Result::Ok as stdOk;
const CONFIG_PATH: &str = "config_api.json";
const SOCKET_PATH: &str = "api-grub.sock";
// todo! rewrite to use current_exe // todo! rewrite to use current_exe
pub async fn pull_local_config() -> Result<ApiConfig> { pub async fn pull_local_config() -> Result<ApiConfig> {
let conf_path = std::env::current_exe()?; // let conf_path = std::env::current_exe()?;
let path = Path::new("config_api.json"); let path = Path::new(CONFIG_PATH);
// return match conf_path.parent() { // return match conf_path.parent() {
// Some(dir) => { // Some(dir) => {
// let config: ApiConfig = from_str( // let config: ApiConfig = from_str(
// &fs::read_to_string(dir.join("config_api.json"))? // &fs::read_to_string(dir.join(CONFIG_PATH))?
// )?; // )?;
// Ok(config) // Ok(config)
// }, // },
@ -26,7 +28,7 @@ pub async fn pull_local_config() -> Result<ApiConfig> {
// } // }
if path.exists() && path.is_file() { if path.exists() && path.is_file() {
let config: ApiConfig = from_str( let config: ApiConfig = from_str(
&fs::read_to_string("config_api.json")? &fs::read_to_string(CONFIG_PATH)?
)?; )?;
Ok(config) Ok(config)
} else { } else {
@ -36,7 +38,8 @@ pub async fn pull_local_config() -> Result<ApiConfig> {
// for config pulling // for config pulling
// ++++ reader to channel // ++++ reader to channel
async fn init_config_grub_mechanism(config: ApiConfig) -> Result<()> { pub async fn init_config_grub_mechanism() -> Result<()> {
info!("Initializing Unix-Socket listening for pulling new configs...");
let server = init_unix_listener().await?; let server = init_unix_listener().await?;
// //
info!("Listening Unix-Socket..."); info!("Listening Unix-Socket...");
@ -46,26 +49,33 @@ async fn init_config_grub_mechanism(config: ApiConfig) -> Result<()> {
if let stdOk((mut stream, _)) = server.accept().await { if let stdOk((mut stream, _)) = server.accept().await {
if let Err(er) = stream.read_to_string(&mut buffer).await { if let Err(er) = stream.read_to_string(&mut buffer).await {
warn!("Cannot read config from stream due to {}", er); warn!("Cannot read config from stream due to {}", er);
sleep(Duration::from_millis(500)).await;
} else { } else {
let config: Result<ApiConfig, serde_json::Error> = from_str(&buffer); let config: Result<ApiConfig, serde_json::Error> = from_str(&buffer);
if let stdOk(conf) = config { if let stdOk(_conf) = config {
info!("New config was pulled from Unix-Stream. Saving it locally...");
} else { if let Err(er) = save_new_config(&buffer).await {
error!("Cannot save new config locally due to: {}", er);
}
// TODO!
// reading new config to channel
// saving new config locally
} else if let Err(er) = config {
warn!("Invalid config was pulled. Error: {}", er);
} }
} }
} }
sleep(Duration::from_millis(500)).await;
} }
//
Ok(())
} }
async fn save_new_config() -> Result<()> {
// saving new config locally
async fn save_new_config(config: &String) -> Result<()> {
fs::write(CONFIG_PATH, config)?;
Ok(()) Ok(())
} }
async fn init_unix_listener() -> Result<UnixListener> { async fn init_unix_listener() -> Result<UnixListener> {
fs::remove_file("api-grub.sock")?; let _ = fs::remove_file(SOCKET_PATH);
Ok(UnixListener::bind("api-grub.sock")?) Ok(UnixListener::bind(SOCKET_PATH)?)
} }

View File

@ -6,7 +6,8 @@ use anyhow::Result;
use integr_structs::api::ApiConfig; use integr_structs::api::ApiConfig;
use logger::setup_logger; use logger::setup_logger;
use log::{info, warn}; use log::{info, warn};
use config::pull_local_config; use config::{pull_local_config, init_config_grub_mechanism};
use net::init_api_grub_mechanism;
#[tokio::main(flavor = "multi_thread")] #[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()>{ async fn main() -> Result<()>{
@ -16,6 +17,13 @@ async fn main() -> Result<()>{
// 3) ? // 3) ?
setup_logger().await?; setup_logger().await?;
let config = get_config().await; let config = get_config().await;
// futures
let config_fut = init_config_grub_mechanism();
let grub_fut = init_api_grub_mechanism(&config);
let _ = tokio::join!(config_fut, grub_fut);
Ok(()) Ok(())
} }

View File

@ -1,9 +1,15 @@
// module to handle unix-socket connection + pulling info from api // module to handle unix-socket connection + pulling info from api
use anyhow; use anyhow::Result;
use integr_structs::api::ApiConfig;
use log::info;
// for api info pulling // for api info pulling
async fn init_api_grub_mechanism() { pub async fn init_api_grub_mechanism(_config: &ApiConfig) -> Result<()> {
info!("Initializing API-info grubbing mechanism...");
Ok(())
} }
// one-time exec func to send request, deserialize it and return object
#[allow(dead_code)]
async fn send_api_request() -> Result<()> {Ok(())}