bugged: config v3
parent
8362b50bcb
commit
3ebf40aa64
|
|
@ -10,12 +10,13 @@ use tokio::{io::AsyncReadExt, net::UnixListener};
|
||||||
use tokio::time::{sleep, Duration};
|
use tokio::time::{sleep, Duration};
|
||||||
use std::result::Result::Ok as stdOk;
|
use std::result::Result::Ok as stdOk;
|
||||||
use tokio::sync::mpsc::Sender;
|
use tokio::sync::mpsc::Sender;
|
||||||
|
use integr_structs::api::v3::Config;
|
||||||
|
|
||||||
const CONFIG_PATH: &str = "config_api.json";
|
const CONFIG_PATH: &str = "config_api.json";
|
||||||
const SOCKET_PATH: &str = "api-grub.sock";
|
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<ApiConfigV2> {
|
pub async fn pull_local_config() -> Result<Config> {
|
||||||
// let conf_path = std::env::current_exe()?;
|
// let conf_path = std::env::current_exe()?;
|
||||||
let path = Path::new(CONFIG_PATH);
|
let path = Path::new(CONFIG_PATH);
|
||||||
// return match conf_path.parent() {
|
// return match conf_path.parent() {
|
||||||
|
|
@ -28,7 +29,7 @@ pub async fn pull_local_config() -> Result<ApiConfigV2> {
|
||||||
// None => Err(Error::msg("No local conf was found"))
|
// None => Err(Error::msg("No local conf was found"))
|
||||||
// }
|
// }
|
||||||
if path.exists() && path.is_file() {
|
if path.exists() && path.is_file() {
|
||||||
let config: ApiConfigV2 = from_str(
|
let config: Config = from_str(
|
||||||
&fs::read_to_string(CONFIG_PATH)?
|
&fs::read_to_string(CONFIG_PATH)?
|
||||||
)?;
|
)?;
|
||||||
Ok(config)
|
Ok(config)
|
||||||
|
|
@ -39,7 +40,7 @@ pub async fn pull_local_config() -> Result<ApiConfigV2> {
|
||||||
|
|
||||||
// for config pulling
|
// for config pulling
|
||||||
// ++++ reader to channel
|
// ++++ reader to channel
|
||||||
pub async fn init_config_grub_mechanism(tx: &Sender<ApiConfigV2>) -> Result<()> {
|
pub async fn init_config_grub_mechanism(tx: &Sender<Config>) -> Result<()> {
|
||||||
info!("Initializing Unix-Socket listening for pulling new configs...");
|
info!("Initializing Unix-Socket listening for pulling new configs...");
|
||||||
let server = init_unix_listener().await?;
|
let server = init_unix_listener().await?;
|
||||||
//
|
//
|
||||||
|
|
@ -51,7 +52,7 @@ pub async fn init_config_grub_mechanism(tx: &Sender<ApiConfigV2>) -> Result<()>
|
||||||
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);
|
||||||
} else {
|
} else {
|
||||||
let config: Result<ApiConfigV2, serde_json::Error> = from_str(&buffer);
|
let config: Result<Config, 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 and sharing with API-grub module...");
|
info!("New config was pulled from Unix-Stream. Saving it locally and sharing with API-grub module...");
|
||||||
if let Err(er) = save_new_config(&buffer).await {
|
if let Err(er) = save_new_config(&buffer).await {
|
||||||
|
|
@ -97,13 +98,13 @@ mod config_unittests {
|
||||||
#[test]
|
#[test]
|
||||||
async fn check_save_new_config() {
|
async fn check_save_new_config() {
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use integr_structs::api::ApiConfigV2;
|
use integr_structs::api::v3::Config;
|
||||||
use serde_json::to_string;
|
use serde_json::to_string;
|
||||||
|
|
||||||
let test_config_path = "test_config_api.json";
|
let test_config_path = "test_config_api.json";
|
||||||
|
|
||||||
// config gen
|
// config gen
|
||||||
let config = to_string::<ApiConfigV2>(&ApiConfigV2::default());
|
let config = to_string::<Config>(&Config::default());
|
||||||
assert!(config.is_ok());
|
assert!(config.is_ok());
|
||||||
let config = config.unwrap();
|
let config = config.unwrap();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ mod export;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use integr_structs::api::ApiConfigV2;
|
use integr_structs::api::ApiConfigV2;
|
||||||
|
use integr_structs::api::v3::Config;
|
||||||
use logger::setup_logger;
|
use logger::setup_logger;
|
||||||
// use log::{info, warn};
|
// use log::{info, warn};
|
||||||
use config::{pull_local_config, init_config_grub_mechanism};
|
use config::{pull_local_config, init_config_grub_mechanism};
|
||||||
|
|
@ -57,7 +58,7 @@ async fn main() -> Result<()>{
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_config() -> ApiConfigV2 {
|
async fn get_config() -> Config {
|
||||||
return match pull_local_config().await {
|
return match pull_local_config().await {
|
||||||
Ok(conf) => {
|
Ok(conf) => {
|
||||||
info!("Local config was loaded");
|
info!("Local config was loaded");
|
||||||
|
|
@ -65,7 +66,7 @@ async fn get_config() -> ApiConfigV2 {
|
||||||
},
|
},
|
||||||
Err(er) => {
|
Err(er) => {
|
||||||
warn!("Cannot get local config due to {}", er);
|
warn!("Cannot get local config due to {}", er);
|
||||||
ApiConfigV2::default()
|
Config::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -150,14 +150,14 @@ pub mod v3 {
|
||||||
pub use super::*;
|
pub use super::*;
|
||||||
|
|
||||||
// in config
|
// in config
|
||||||
#[derive(Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Metric {
|
pub struct Metric {
|
||||||
pub id : String,
|
pub id : String,
|
||||||
#[serde(rename = "type")]
|
#[serde(rename = "type")]
|
||||||
pub json_type : String,
|
pub json_type : String,
|
||||||
pub addr : String,
|
pub addr : String,
|
||||||
}
|
}
|
||||||
#[derive(Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Metrics {
|
pub struct Metrics {
|
||||||
pub name : String,
|
pub name : String,
|
||||||
pub url : String,
|
pub url : String,
|
||||||
|
|
@ -165,7 +165,7 @@ pub mod v3 {
|
||||||
pub measure : Vec<Metric>
|
pub measure : Vec<Metric>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct ConfigEndpoint {
|
pub struct ConfigEndpoint {
|
||||||
ip : String,
|
ip : String,
|
||||||
login : String,
|
login : String,
|
||||||
|
|
@ -178,7 +178,7 @@ pub mod v3 {
|
||||||
metrics : Vec<Metrics>,
|
metrics : Vec<Metrics>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
config : Vec<ConfigEndpoint>,
|
config : Vec<ConfigEndpoint>,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue