master
prplV 2025-02-18 13:34:09 +03:00
parent f503d2952b
commit 6f9bb82136
6 changed files with 141 additions and 4 deletions

7
.env.example Normal file
View File

@ -0,0 +1,7 @@
# Template .env
# Url to Monitoring-System (main configs distributor) web-socket
CONFIG_SERVER_CREDS = "ws://ip.ip.ip.ip:port"
# Path (full or right relative) to the API-grabber socket file
# Using internal HTTP API across Unix-Socket
API_GRUBBER_SOCKET = "api-grub.sock"

View File

@ -10,7 +10,6 @@ serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.135"
tokio = { version = "1.43.0", features = ["full"] }
tokio-websockets = { version = "^0.11.0", features = ["client", "openssl", "rand"] }
integr-structs = {path = "../integr-structs"}
anyhow = "1.0.95"
env_logger = "0.11.6"
log = "0.4.25"

View File

@ -2,7 +2,8 @@
// using Unix-Socket Client
use anyhow::{Error, Result};
use integr_structs::api::ApiConfigV2;
// use integr_structs::api::ApiConfigV2;
use crate::structs::ApiConfigV2;
use tokio::time::{sleep, Duration};
use tokio::net::UnixStream;
use std::env;

View File

@ -1,7 +1,8 @@
use std::sync::Arc;
use tokio::sync::mpsc::Sender;
use integr_structs::api::ApiConfigV2;
// use integr_structs::api::ApiConfigV2;
use crate::structs::ApiConfigV2;
use anyhow::Result;
use tokio::time::{sleep, Duration};
// mock

View File

@ -1,8 +1,10 @@
mod delivery;
mod integration;
mod logger;
mod structs;
use integr_structs::api::ApiConfigV2;
// use integr_structs::api::ApiConfigV2;
use structs::ApiConfigV2;
use logger::setup_logger;
use dotenv::dotenv;
use anyhow::Result;

127
src/structs.rs Normal file
View File

@ -0,0 +1,127 @@
use core::sync;
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use serde_json::{ to_string_pretty, Value };
use anyhow::Result;
use std::sync::Arc;
// v2
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct ApiConfigV2 {
pub id : u64,
#[serde(default)]
pub template : Vec<Template>,
pub ip_address : String,
pub login : Option<String>,
pub pass : Option<String>,
pub api_key : Option<String>,
pub period : u32, // if "0" -> inf
pub timeout : u32, // if "0" -> no-delay
}
impl Default for ApiConfigV2 {
fn default() -> Self {
ApiConfigV2 {
id : 0,
template : Vec::new(),
ip_address : String::from("no_ip"),
login : None,
pass : None,
api_key : None,
period : 0,
timeout : 0,
}
}
}
impl ApiConfigV2 {
pub fn template() -> Self {
ApiConfigV2 {
id : 1111,
template : Vec::new(),
ip_address : String::from("ip"),
login : None,
pass : None,
api_key : None,
period : 1111,
timeout : 1111,
}
}
pub fn pattern() -> Self {
ApiConfigV2 {
id : 1111,
template : vec![
Template {
id : String::from("no id"),
name : String::from("open api"),
url : String::from("https://dummy-json.mock.beeceptor.com/countries"),
method : String::from("GET"),
measure : Vec::new(),
}
],
ip_address : String::from("ip"),
login : None,
pass : None,
api_key : None,
period : 1,
timeout : 1,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Template {
pub id : String,
pub name : String,
pub url : String,
pub method : String,
#[serde(default)]
pub measure : Vec<String>,
}
impl Default for Template {
fn default() -> Self {
Template {
id : String::from("no-id"),
name : String::from("no-name"),
url : String::from("no-url"),
method : String::from("post"),
measure : Vec::new(),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ProcessedEndpoint {
id : String,
name : String,
url : String,
method : String,
#[serde(default)]
metrics : HashMap<String, Value>,
}
impl ProcessedEndpoint {
pub fn new(id: &str, name: &str, url: &str, method: &str, metrics: HashMap<String, Value>) -> Self {
ProcessedEndpoint {
id : id.to_owned(),
name : name.to_owned(),
url : url.to_owned(),
method : method.to_owned(),
metrics : metrics,
}
}
pub fn from_target_response(response: &str, keys: &Template) -> Result<String> {
let mut hm: HashMap<String, Value> = HashMap::new();
let mut response: Value = serde_json::from_str(response)?;
let _ = keys.measure.iter()
.map(|key| (key, response[key].take()))
.for_each(|(key, value)| {
hm.insert(key.clone(), value);
});
let val = ProcessedEndpoint::new(&keys.id, &keys.name, &keys.url, &keys.method,hm);
Ok(to_string_pretty(&val)?)
}
}