unixsocket fix + disable lint warnings
parent
4c88b27d82
commit
991bae50f6
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "api-grub"
|
||||
version = "0.1.0"
|
||||
version = "0.3.8"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
|
|
|||
|
|
@ -3,22 +3,24 @@
|
|||
// 2) save changes in local config file
|
||||
use integr_structs::api::ApiConfig;
|
||||
use anyhow::{Error, Ok, Result};
|
||||
use log::{info, warn};
|
||||
use log::{info, warn, error};
|
||||
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;
|
||||
|
||||
const CONFIG_PATH: &str = "config_api.json";
|
||||
const SOCKET_PATH: &str = "api-grub.sock";
|
||||
|
||||
// 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");
|
||||
// let conf_path = std::env::current_exe()?;
|
||||
let path = Path::new(CONFIG_PATH);
|
||||
// return match conf_path.parent() {
|
||||
// Some(dir) => {
|
||||
// let config: ApiConfig = from_str(
|
||||
// &fs::read_to_string(dir.join("config_api.json"))?
|
||||
// &fs::read_to_string(dir.join(CONFIG_PATH))?
|
||||
// )?;
|
||||
// Ok(config)
|
||||
// },
|
||||
|
|
@ -26,7 +28,7 @@ pub async fn pull_local_config() -> Result<ApiConfig> {
|
|||
// }
|
||||
if path.exists() && path.is_file() {
|
||||
let config: ApiConfig = from_str(
|
||||
&fs::read_to_string("config_api.json")?
|
||||
&fs::read_to_string(CONFIG_PATH)?
|
||||
)?;
|
||||
Ok(config)
|
||||
} else {
|
||||
|
|
@ -36,7 +38,8 @@ pub async fn pull_local_config() -> Result<ApiConfig> {
|
|||
|
||||
// for config pulling
|
||||
// ++++ 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?;
|
||||
//
|
||||
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 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 {
|
||||
|
||||
if let stdOk(_conf) = config {
|
||||
info!("New config was pulled from Unix-Stream. Saving it locally...");
|
||||
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(())
|
||||
}
|
||||
|
||||
|
||||
async fn init_unix_listener() -> Result<UnixListener> {
|
||||
fs::remove_file("api-grub.sock")?;
|
||||
Ok(UnixListener::bind("api-grub.sock")?)
|
||||
let _ = fs::remove_file(SOCKET_PATH);
|
||||
Ok(UnixListener::bind(SOCKET_PATH)?)
|
||||
}
|
||||
|
|
@ -6,7 +6,8 @@ use anyhow::Result;
|
|||
use integr_structs::api::ApiConfig;
|
||||
use logger::setup_logger;
|
||||
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")]
|
||||
async fn main() -> Result<()>{
|
||||
|
|
@ -16,6 +17,13 @@ async fn main() -> Result<()>{
|
|||
// 3) ?
|
||||
setup_logger().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(())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,15 @@
|
|||
// 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
|
||||
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(())}
|
||||
Loading…
Reference in New Issue