mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-30 23:08:40 +08:00
Axum api server with json and "conf" file support.
This commit is contained in:
@@ -5,6 +5,7 @@ use std::fs;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crate::web::webserver;
|
||||
use async_trait::async_trait;
|
||||
use notify::event::ModifyKind;
|
||||
use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
|
||||
@@ -14,9 +15,8 @@ use tokio::task;
|
||||
pub struct FromFileProvider {
|
||||
pub path: String,
|
||||
}
|
||||
pub struct APIUpstreamProvider {
|
||||
pub api_url: String,
|
||||
}
|
||||
pub struct APIUpstreamProvider;
|
||||
|
||||
#[async_trait]
|
||||
pub trait Discovery {
|
||||
async fn run(&self, tx: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>);
|
||||
@@ -24,16 +24,15 @@ pub trait Discovery {
|
||||
|
||||
#[async_trait]
|
||||
impl Discovery for APIUpstreamProvider {
|
||||
async fn run(&self, mut toreturn: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
||||
async fn run(&self, toreturn: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
||||
let _ = tokio::spawn(async move { webserver::run_server(toreturn).await });
|
||||
loop {
|
||||
let dm: DashMap<String, (Vec<(String, u16)>, AtomicUsize)> = DashMap::new();
|
||||
dm.insert(
|
||||
self.api_url.to_string(),
|
||||
(vec![("192.168.1.1".parse().unwrap(), 8000), ("192.168.1.10".parse().unwrap(), 8000)], AtomicUsize::new(0)),
|
||||
);
|
||||
println!("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ");
|
||||
let _ = toreturn.send(dm).await.unwrap();
|
||||
println!("= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ");
|
||||
// let dm: DashMap<String, (Vec<(String, u16)>, AtomicUsize)> = DashMap::new();
|
||||
// dm.insert(
|
||||
// "popok.netangels.net".to_string(),
|
||||
// (vec![("192.168.1.1".parse().unwrap(), 8000), ("192.168.1.10".parse().unwrap(), 8000)], AtomicUsize::new(0)),
|
||||
// );
|
||||
// let _ = toreturn.send(dm).await.unwrap();
|
||||
tokio::time::sleep(Duration::from_secs(20)).await;
|
||||
}
|
||||
}
|
||||
@@ -56,7 +55,7 @@ pub async fn watch_file(fp: String, mut toreturn: Sender<DashMap<String, (Vec<(S
|
||||
println!(" {}", path.unwrap().path().display())
|
||||
}
|
||||
|
||||
let snd = read_upstreams_from_file(file_path);
|
||||
let snd = read_upstreams_from_file(file_path, "filepath");
|
||||
let _ = toreturn.send(snd).await.unwrap();
|
||||
|
||||
let _watcher_handle = task::spawn_blocking({
|
||||
@@ -76,10 +75,6 @@ pub async fn watch_file(fp: String, mut toreturn: Sender<DashMap<String, (Vec<(S
|
||||
}
|
||||
}
|
||||
});
|
||||
// loop {
|
||||
// println!(" ---------------------------------------------------------------- ");
|
||||
// thread::sleep(Duration::from_secs(1));
|
||||
// }
|
||||
let mut start = Instant::now();
|
||||
|
||||
while let Some(event) = local_rx.recv().await {
|
||||
@@ -92,26 +87,36 @@ pub async fn watch_file(fp: String, mut toreturn: Sender<DashMap<String, (Vec<(S
|
||||
start = Instant::now();
|
||||
println!("Config File changed :=> {:?}", e);
|
||||
|
||||
let snd = read_upstreams_from_file(file_path);
|
||||
let snd = read_upstreams_from_file(file_path, "filepath");
|
||||
let _ = toreturn.send(snd).await.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (), //println!("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *"),
|
||||
_ => (),
|
||||
},
|
||||
Err(e) => println!("Watch error: {:?}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
fn read_upstreams_from_file(path: &str) -> DashMap<String, (Vec<(String, u16)>, AtomicUsize)> {
|
||||
pub fn read_upstreams_from_file(d: &str, kind: &str) -> DashMap<String, (Vec<(String, u16)>, AtomicUsize)> {
|
||||
let upstreams = DashMap::new();
|
||||
let contents = match fs::read_to_string(path) {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file: {:?}", e);
|
||||
return upstreams;
|
||||
let mut contents = d.to_string();
|
||||
match kind {
|
||||
"filepath" => {
|
||||
println!("Reading upstreams from {}", d);
|
||||
let _ = match fs::read_to_string(d) {
|
||||
Ok(data) => contents = data,
|
||||
Err(e) => {
|
||||
eprintln!("Error reading file: {:?}", e);
|
||||
return upstreams;
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
"content" => {
|
||||
println!("Reading upstreams from API post body");
|
||||
}
|
||||
_ => println!("*******************> nothing <*******************"),
|
||||
}
|
||||
|
||||
for line in contents.lines().filter(|line| !line.trim().is_empty()) {
|
||||
let mut parts = line.split_whitespace();
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod proxyhttp;
|
||||
pub mod start;
|
||||
pub mod webserver;
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
// use crate::utils::compare;
|
||||
// use crate::utils::discovery;
|
||||
use crate::utils::discovery::{APIUpstreamProvider, Discovery, FromFileProvider};
|
||||
use crate::utils::*;
|
||||
use async_trait::async_trait;
|
||||
@@ -21,26 +19,19 @@ pub struct LB {
|
||||
pub upstreams: Arc<RwLock<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>>,
|
||||
pub umap_full: Arc<RwLock<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>>,
|
||||
}
|
||||
// pub struct BGService {
|
||||
// pub upstreams: Arc<RwLock<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>>,
|
||||
// pub umap_full: Arc<RwLock<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>>,
|
||||
// }
|
||||
|
||||
#[async_trait]
|
||||
impl BackgroundService for LB {
|
||||
async fn start(&self, mut shutdown: ShutdownWatch) {
|
||||
tokio::spawn(healthcheck::hc(self.upstreams.clone(), self.umap_full.clone()));
|
||||
println!("Starting example background service");
|
||||
// let (tra, mut rec) = broadcast::channel::<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>(16);
|
||||
|
||||
let (tx, mut rx) = mpsc::channel::<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>(0);
|
||||
let file_load = FromFileProvider {
|
||||
path: "etc/upstreams.conf".to_string(),
|
||||
};
|
||||
|
||||
let api_load = APIUpstreamProvider {
|
||||
api_url: "myip.netangels.net".to_string(),
|
||||
};
|
||||
let api_load = APIUpstreamProvider;
|
||||
|
||||
let tx_file = tx.clone();
|
||||
let tx_api = tx.clone();
|
||||
@@ -52,23 +43,19 @@ impl BackgroundService for LB {
|
||||
_ = shutdown.changed() => {
|
||||
break;
|
||||
}
|
||||
// _ = period.tick() => {
|
||||
val = rx.next() => {
|
||||
match val {
|
||||
Some(newmap) => {
|
||||
println!("{:?}", newmap);
|
||||
let umap_work = self.upstreams.write().await;
|
||||
let umap_full = self.umap_full.write().await;
|
||||
if !compare::dashmaps(&umap_full, &newmap) {
|
||||
println!("DashMaps are different. Syncing !!!!!");
|
||||
umap_work.clear();
|
||||
umap_full.clear();
|
||||
for (k,v) in newmap {
|
||||
println!("Host: {}", k);
|
||||
for vv in v.0.clone() {
|
||||
println!(" Upstreams: {:?}", vv);
|
||||
println!(" ===> {:?}", vv);
|
||||
}
|
||||
// println!("{} -> {:?}", k, v);
|
||||
umap_work.insert(k.clone(), (v.0.clone(), AtomicUsize::new(0))); // No need for extra vec!
|
||||
umap_full.insert(k, (v.0, AtomicUsize::new(0))); // Use `value.0` directly
|
||||
}
|
||||
@@ -92,9 +79,6 @@ pub trait GetHost {
|
||||
impl GetHost for LB {
|
||||
async fn get_host(&self, peer: &str) -> Option<(String, u16)> {
|
||||
let map_read = self.upstreams.read().await;
|
||||
// let ful_read = self.umap_full.read().await;
|
||||
// println!("DN ==> {:?}", map_read);
|
||||
// println!("FU ==> {:?}", ful_read);
|
||||
let x = if let Some(entry) = map_read.get(peer) {
|
||||
let (servers, index) = entry.value(); // No clone here
|
||||
|
||||
|
||||
69
src/web/webserver.rs
Normal file
69
src/web/webserver.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use axum::body::Body;
|
||||
use axum::http::{Response, StatusCode};
|
||||
use axum::routing::post;
|
||||
use axum::{routing::get, Json, Router};
|
||||
use dashmap::DashMap;
|
||||
use futures::channel::mpsc::Sender;
|
||||
use futures::SinkExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct UpstreamData {
|
||||
servers: Vec<(String, u16)>,
|
||||
counter: usize,
|
||||
}
|
||||
|
||||
pub async fn run_server(mut toreturn: Sender<DashMap<String, (Vec<(String, u16)>, AtomicUsize)>>) {
|
||||
let mut tr = toreturn.clone();
|
||||
let app = Router::new()
|
||||
.route("/", get(getconfig))
|
||||
.route(
|
||||
"/conf",
|
||||
post(|up: String| async move {
|
||||
let serverlist = crate::utils::discovery::read_upstreams_from_file(up.as_str(), "content");
|
||||
let _ = tr.send(serverlist).await.unwrap();
|
||||
Response::builder().status(StatusCode::CREATED).body(Body::from("Config, conf file, updated!\n")).unwrap()
|
||||
})
|
||||
.with_state("state"),
|
||||
)
|
||||
.route(
|
||||
"/json",
|
||||
post(|Json(payload): Json<HashMap<String, UpstreamData>>| async move {
|
||||
let upstreams = DashMap::new();
|
||||
for (key, value) in payload {
|
||||
upstreams.insert(key, (value.servers, AtomicUsize::new(value.counter)));
|
||||
}
|
||||
let _ = toreturn.send(upstreams).await.unwrap();
|
||||
Response::builder().status(StatusCode::CREATED).body(Body::from("Config, json, updated!\n")).unwrap()
|
||||
}),
|
||||
);
|
||||
let listener = TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||
println!("Axum API server running on port 3000");
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn getconfig() -> String {
|
||||
"Hello from Axum API inside Pingora!\n".to_string()
|
||||
}
|
||||
// curl -XPOST -H 'Content-Type: application/json' --data-binary @./push.json 127.0.0.1:3000/json
|
||||
// curl -XPOST --data-binary @./etc/upstreams.txt 127.0.0.1:3000/conf
|
||||
|
||||
/*
|
||||
async fn config(Json(payload): Json<HashMap<String, UpstreamData>>) -> impl IntoResponse {
|
||||
let upstreams = DashMap::new();
|
||||
for (key, value) in payload {
|
||||
upstreams.insert(key, (value.servers, AtomicUsize::new(value.counter)));
|
||||
}
|
||||
println!("{:?}", upstreams);
|
||||
Response::builder().status(StatusCode::CREATED).body(Body::from("Config updated!\n")).unwrap()
|
||||
}
|
||||
async fn parse_upstreams(up: String) -> impl IntoResponse {
|
||||
println!("Parsing: {}", up);
|
||||
let serverlist = read_upstreams_from_file(up.as_str());
|
||||
println!("{:?}", serverlist);
|
||||
Response::builder().status(StatusCode::CREATED).body(Body::from("Config updated!\n")).unwrap()
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user