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