Compare commits
No commits in common. "991bae50f6127fecb112ab7269cf2022e9596a0a" and "60d325e4bf6021ba569ce07ae01b8e81ec6617c5" have entirely different histories.
991bae50f6
...
60d325e4bf
|
|
@ -1,13 +0,0 @@
|
|||
{
|
||||
"endpoints" : [
|
||||
{
|
||||
"url" : "http://127.0.0.1:8081/ping",
|
||||
"method" : "GET"
|
||||
},
|
||||
{
|
||||
"url" : "http://127.0.0.1:8081/",
|
||||
"method" : "GET"
|
||||
}
|
||||
],
|
||||
"delay" : 5
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "api-grub"
|
||||
version = "0.3.8"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"api-endpoint" : "http://127.0.0.1:8081/ping",
|
||||
"method" : "GET",
|
||||
"delay" : "5"
|
||||
}
|
||||
|
|
@ -2,80 +2,13 @@
|
|||
// 1) check changes in unix-socket
|
||||
// 2) save changes in local config file
|
||||
use integr_structs::api::ApiConfig;
|
||||
use anyhow::{Error, Ok, Result};
|
||||
use log::{info, warn, error};
|
||||
use std::{fs, path::Path};
|
||||
use serde_json::from_str;
|
||||
use tokio::{io::AsyncReadExt, net::UnixListener};
|
||||
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_PATH);
|
||||
// return match conf_path.parent() {
|
||||
// Some(dir) => {
|
||||
// let config: ApiConfig = from_str(
|
||||
// &fs::read_to_string(dir.join(CONFIG_PATH))?
|
||||
// )?;
|
||||
// Ok(config)
|
||||
// },
|
||||
// None => Err(Error::msg("No local conf was found"))
|
||||
// }
|
||||
if path.exists() && path.is_file() {
|
||||
let config: ApiConfig = from_str(
|
||||
&fs::read_to_string(CONFIG_PATH)?
|
||||
)?;
|
||||
Ok(config)
|
||||
} else {
|
||||
Err(Error::msg("No local conf was found"))
|
||||
}
|
||||
}
|
||||
use tokio::net::UnixListener;
|
||||
use anyhow::Result;
|
||||
|
||||
// for config pulling
|
||||
// ++++ reader to channel
|
||||
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...");
|
||||
let mut buffer = String::new();
|
||||
//
|
||||
loop {
|
||||
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);
|
||||
} else {
|
||||
let config: Result<ApiConfig, serde_json::Error> = from_str(&buffer);
|
||||
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;
|
||||
}
|
||||
}
|
||||
async fn init_api_grub_mechanism(config: ApiConfig) {
|
||||
|
||||
// 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> {
|
||||
let _ = fs::remove_file(SOCKET_PATH);
|
||||
Ok(UnixListener::bind(SOCKET_PATH)?)
|
||||
}
|
||||
async fn init_unix_listener() {}
|
||||
|
|
@ -3,9 +3,8 @@ use env_logger::Builder;
|
|||
use log::LevelFilter;
|
||||
use std::io::Write;
|
||||
use anyhow::Result;
|
||||
use log::info;
|
||||
|
||||
pub async fn setup_logger() -> Result<()> {
|
||||
pub fn setup_logger() -> Result<()> {
|
||||
Builder::new()
|
||||
.format(move |buf, record| {
|
||||
writeln!(
|
||||
|
|
@ -21,6 +20,5 @@ pub async fn setup_logger() -> Result<()> {
|
|||
.target(env_logger::Target::Stdout)
|
||||
.init();
|
||||
|
||||
info!("Logger configured");
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -3,11 +3,8 @@ mod net;
|
|||
mod logger;
|
||||
|
||||
use anyhow::Result;
|
||||
use integr_structs::api::ApiConfig;
|
||||
use logger::setup_logger;
|
||||
use log::{info, warn};
|
||||
use config::{pull_local_config, init_config_grub_mechanism};
|
||||
use net::init_api_grub_mechanism;
|
||||
use log::info;
|
||||
|
||||
#[tokio::main(flavor = "multi_thread")]
|
||||
async fn main() -> Result<()>{
|
||||
|
|
@ -15,27 +12,7 @@ async fn main() -> Result<()>{
|
|||
// 1) unix-socket coroutine (for config updating)
|
||||
// 2) api coroutine
|
||||
// 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);
|
||||
|
||||
setup_logger()?;
|
||||
info!("Logger configured");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_config() -> ApiConfig {
|
||||
return match pull_local_config().await {
|
||||
Ok(conf) => {
|
||||
info!("Local config was loaded");
|
||||
conf
|
||||
},
|
||||
Err(er) => {
|
||||
warn!("Cannot get local config due to {}", er);
|
||||
ApiConfig::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +1,9 @@
|
|||
// module to handle unix-socket connection + pulling info from api
|
||||
use anyhow::Result;
|
||||
use integr_structs::api::ApiConfig;
|
||||
use log::info;
|
||||
use anyhow;
|
||||
|
||||
|
||||
// for api info pulling
|
||||
pub async fn init_api_grub_mechanism(_config: &ApiConfig) -> Result<()> {
|
||||
info!("Initializing API-info grubbing mechanism...");
|
||||
Ok(())
|
||||
async fn init_api_grub_mechanism() {
|
||||
|
||||
}
|
||||
|
||||
// one-time exec func to send request, deserialize it and return object
|
||||
#[allow(dead_code)]
|
||||
async fn send_api_request() -> Result<()> {Ok(())}
|
||||
|
|
@ -1,23 +1,19 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ApiConfig {
|
||||
#[serde(default)]
|
||||
endpoints : Vec<ApiEndpoint>,
|
||||
url : String,
|
||||
method : String,
|
||||
delay : u32,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct ApiEndpoint {
|
||||
url : String,
|
||||
method : String,
|
||||
}
|
||||
|
||||
impl Default for ApiConfig {
|
||||
fn default() -> Self {
|
||||
ApiConfig {
|
||||
endpoints : vec![],
|
||||
url : String::new(),
|
||||
method : String::new(),
|
||||
delay : 0,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue