mirror of
https://github.com/sadoyan/aralez.git
synced 2026-04-29 22:38:36 +08:00
reStructurization of files
This commit is contained in:
@@ -5,4 +5,5 @@ mod filewatch;
|
||||
pub mod healthcheck;
|
||||
pub mod jwt;
|
||||
pub mod parceyaml;
|
||||
pub mod structs;
|
||||
pub mod tools;
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
use crate::utils::parceyaml::{load_configuration, Configuration, ServiceMapping};
|
||||
use crate::utils::tools::{clone_dashmap_into, compare_dashmaps, UpstreamsDashMap};
|
||||
use crate::utils::parceyaml::load_configuration;
|
||||
use crate::utils::structs::{Configuration, ServiceMapping, UpstreamsDashMap};
|
||||
use crate::utils::tools::{clone_dashmap_into, compare_dashmaps};
|
||||
use dashmap::DashMap;
|
||||
use futures::channel::mpsc::Sender;
|
||||
use futures::SinkExt;
|
||||
// use hickory_client::client::{Client, ClientHandle};
|
||||
// use hickory_client::proto::rr::{DNSClass, Name, RecordType};
|
||||
// use hickory_client::proto::runtime::TokioRuntimeProvider;
|
||||
// use hickory_client::proto::tcp::TcpClientStream;
|
||||
use log::{info, warn};
|
||||
use pingora::prelude::sleep;
|
||||
use rand::Rng;
|
||||
// use std::str::FromStr;
|
||||
use reqwest::header::{HeaderMap, HeaderValue};
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
@@ -19,8 +15,6 @@ use std::time::Duration;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Service {
|
||||
// #[serde(rename = "ServiceName")]
|
||||
// service_name: String,
|
||||
#[serde(rename = "ServiceTaggedAddresses")]
|
||||
tagged_addresses: HashMap<String, TaggedAddress>,
|
||||
}
|
||||
@@ -148,20 +142,3 @@ async fn get_by_http(url: String, token: Option<String>) -> Option<DashMap<Strin
|
||||
upstreams.insert("/".to_string(), (values, AtomicUsize::new(0)));
|
||||
Some(upstreams)
|
||||
}
|
||||
|
||||
// #[allow(dead_code)]
|
||||
// async fn get_by_dns() {
|
||||
// let (stream, sender) = TcpClientStream::new(([192, 168, 22, 1], 53).into(), None, None, TokioRuntimeProvider::new());
|
||||
// let client = Client::new(stream, sender, None);
|
||||
// let (mut client, bg) = client.await.expect("connection failed");
|
||||
// tokio::spawn(bg);
|
||||
// let query = client.query(Name::from_str("_frontend-dev-frontend-srv._tcp.service.consul.").unwrap(), DNSClass::IN, RecordType::SRV);
|
||||
// // let query = client.query(Name::from_str("matyan.org.").unwrap(), DNSClass::IN, RecordType::A);
|
||||
// let response = query.await.unwrap();
|
||||
//
|
||||
// for t in response.answers().iter() {
|
||||
// for y in t.data().as_srv().iter() {
|
||||
// println!(" DNS ==> {:?} : {:?}", y.target().to_utf8(), y.port());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::utils::consul;
|
||||
use crate::utils::filewatch;
|
||||
use crate::utils::parceyaml::Configuration;
|
||||
use crate::utils::structs::Configuration;
|
||||
use crate::web::webserver;
|
||||
use async_trait::async_trait;
|
||||
use futures::channel::mpsc::Sender;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::utils::parceyaml::{load_configuration, Configuration};
|
||||
use crate::utils::parceyaml::load_configuration;
|
||||
use crate::utils::structs::Configuration;
|
||||
use futures::channel::mpsc::Sender;
|
||||
use futures::SinkExt;
|
||||
use log::{error, info};
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::utils::structs::{UpstreamsDashMap, UpstreamsIdMap};
|
||||
use crate::utils::tools::*;
|
||||
use dashmap::DashMap;
|
||||
use log::{error, info, warn};
|
||||
|
||||
@@ -1,59 +1,11 @@
|
||||
use crate::utils::tools::*;
|
||||
use crate::utils::structs::*;
|
||||
use dashmap::DashMap;
|
||||
use log::{error, info, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_yaml::Error;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ServiceMapping {
|
||||
pub proxy: String,
|
||||
pub real: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Extraparams {
|
||||
pub stickysessions: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Consul {
|
||||
pub servers: Option<Vec<String>>,
|
||||
pub services: Option<Vec<ServiceMapping>>,
|
||||
pub token: Option<String>,
|
||||
}
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Config {
|
||||
provider: String,
|
||||
stickysessions: bool,
|
||||
upstreams: Option<HashMap<String, HostConfig>>,
|
||||
globals: Option<HashMap<String, Vec<String>>>,
|
||||
consul: Option<Consul>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct HostConfig {
|
||||
paths: HashMap<String, PathConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct PathConfig {
|
||||
ssl: bool,
|
||||
servers: Vec<String>,
|
||||
headers: Option<Vec<String>>,
|
||||
}
|
||||
pub struct Configuration {
|
||||
pub upstreams: UpstreamsDashMap,
|
||||
pub headers: Headers,
|
||||
pub consul: Option<Consul>,
|
||||
pub typecfg: String,
|
||||
pub extraparams: Extraparams,
|
||||
pub globals: Option<DashMap<String, Vec<String>>>,
|
||||
}
|
||||
|
||||
// pub fn load_configuration(d: &str, kind: &str) -> Option<(UpstreamsDashMap, Headers, String)> {
|
||||
pub fn load_configuration(d: &str, kind: &str) -> Option<Configuration> {
|
||||
let mut toreturn: Configuration = Configuration {
|
||||
upstreams: Default::default(),
|
||||
@@ -66,9 +18,6 @@ pub fn load_configuration(d: &str, kind: &str) -> Option<Configuration> {
|
||||
toreturn.upstreams = UpstreamsDashMap::new();
|
||||
toreturn.headers = Headers::new();
|
||||
|
||||
// let upstreamsmap = UpstreamsDashMap::new();
|
||||
// let headersmap = DashMap::new();
|
||||
|
||||
let mut yaml_data = d.to_string();
|
||||
match kind {
|
||||
"filepath" => {
|
||||
@@ -172,21 +121,6 @@ pub fn load_configuration(d: &str, kind: &str) -> Option<Configuration> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct AppConfig {
|
||||
pub hc_interval: u16,
|
||||
pub hc_method: String,
|
||||
pub upstreams_conf: String,
|
||||
pub log_level: String,
|
||||
pub config_address: String,
|
||||
pub proxy_address_http: String,
|
||||
pub master_key: String,
|
||||
pub proxy_address_tls: Option<String>,
|
||||
pub tls_certificate: Option<String>,
|
||||
pub tls_key_file: Option<String>,
|
||||
}
|
||||
|
||||
// pub fn parce_main_config(path: &str) -> DashMap<String, String> {
|
||||
pub fn parce_main_config(path: &str) -> AppConfig {
|
||||
info!("Parsing configuration");
|
||||
let data = fs::read_to_string(path).unwrap();
|
||||
|
||||
68
src/utils/structs.rs
Normal file
68
src/utils/structs.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use dashmap::DashMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
|
||||
pub type UpstreamsDashMap = DashMap<String, DashMap<String, (Vec<(String, u16, bool)>, AtomicUsize)>>;
|
||||
pub type Headers = DashMap<String, DashMap<String, Vec<(String, String)>>>;
|
||||
pub type UpstreamsIdMap = DashMap<String, (String, u16, bool)>;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ServiceMapping {
|
||||
pub proxy: String,
|
||||
pub real: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Extraparams {
|
||||
pub stickysessions: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Consul {
|
||||
pub servers: Option<Vec<String>>,
|
||||
pub services: Option<Vec<ServiceMapping>>,
|
||||
pub token: Option<String>,
|
||||
}
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub provider: String,
|
||||
pub stickysessions: bool,
|
||||
pub upstreams: Option<HashMap<String, HostConfig>>,
|
||||
pub globals: Option<HashMap<String, Vec<String>>>,
|
||||
pub consul: Option<Consul>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct HostConfig {
|
||||
pub paths: HashMap<String, PathConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct PathConfig {
|
||||
pub ssl: bool,
|
||||
pub servers: Vec<String>,
|
||||
pub headers: Option<Vec<String>>,
|
||||
}
|
||||
pub struct Configuration {
|
||||
pub upstreams: UpstreamsDashMap,
|
||||
pub headers: Headers,
|
||||
pub consul: Option<Consul>,
|
||||
pub typecfg: String,
|
||||
pub extraparams: Extraparams,
|
||||
pub globals: Option<DashMap<String, Vec<String>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct AppConfig {
|
||||
pub hc_interval: u16,
|
||||
pub hc_method: String,
|
||||
pub upstreams_conf: String,
|
||||
pub log_level: String,
|
||||
pub config_address: String,
|
||||
pub proxy_address_http: String,
|
||||
pub master_key: String,
|
||||
pub proxy_address_tls: Option<String>,
|
||||
pub tls_certificate: Option<String>,
|
||||
pub tls_key_file: Option<String>,
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::utils::structs::{UpstreamsDashMap, UpstreamsIdMap};
|
||||
use dashmap::DashMap;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::any::type_name;
|
||||
@@ -22,10 +23,6 @@ pub fn print_upstreams(upstreams: &UpstreamsDashMap) {
|
||||
}
|
||||
}
|
||||
|
||||
pub type UpstreamsDashMap = DashMap<String, DashMap<String, (Vec<(String, u16, bool)>, AtomicUsize)>>;
|
||||
pub type Headers = DashMap<String, DashMap<String, Vec<(String, String)>>>;
|
||||
pub type UpstreamsIdMap = DashMap<String, (String, u16, bool)>;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn typeoff<T>(_: T) {
|
||||
let to = type_name::<T>();
|
||||
@@ -44,7 +41,6 @@ pub fn string_to_bool(val: Option<&str>) -> Option<bool> {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn clone_dashmap(original: &UpstreamsDashMap) -> UpstreamsDashMap {
|
||||
let new_map: UpstreamsDashMap = DashMap::new();
|
||||
|
||||
@@ -117,7 +113,6 @@ pub fn compare_dashmaps(map1: &UpstreamsDashMap, map2: &UpstreamsDashMap) -> boo
|
||||
true
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn merge_headers(target: &DashMap<String, Vec<(String, String)>>, source: &DashMap<String, Vec<(String, String)>>) {
|
||||
for entry in source.iter() {
|
||||
let global_key = entry.key().clone();
|
||||
@@ -137,10 +132,8 @@ pub fn clone_idmap_into(original: &UpstreamsDashMap, cloned: &UpstreamsIdMap) {
|
||||
let (vec, _) = inner_entry.value();
|
||||
let new_vec = vec.clone();
|
||||
for x in vec.iter() {
|
||||
// let id = format!("{}:{}:{}", x.0.to_string(), x.1.to_string(), x.2.to_string());
|
||||
let mut id = String::new();
|
||||
write!(&mut id, "{}:{}:{}", x.0, x.1, x.2).unwrap();
|
||||
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(id.clone().into_bytes());
|
||||
let hash = hasher.finalize();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::utils::discovery::{APIUpstreamProvider, ConsulProvider, Discovery, FromFileProvider};
|
||||
use crate::utils::parceyaml::Configuration;
|
||||
use crate::utils::structs::Configuration;
|
||||
use crate::utils::tools::*;
|
||||
use crate::utils::*;
|
||||
use crate::web::proxyhttp::LB;
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::utils::auth::authenticate;
|
||||
use crate::utils::parceyaml::{AppConfig, Extraparams};
|
||||
use crate::utils::tools::*;
|
||||
use crate::utils::structs::{AppConfig, Extraparams, Headers, UpstreamsDashMap, UpstreamsIdMap};
|
||||
use crate::web::gethosts::GetHost;
|
||||
use async_trait::async_trait;
|
||||
use dashmap::DashMap;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use crate::utils::parceyaml::Extraparams;
|
||||
use crate::utils::tools::*;
|
||||
use crate::utils::structs::{Extraparams, Headers, UpstreamsDashMap, UpstreamsIdMap};
|
||||
use crate::web::proxyhttp::LB;
|
||||
use dashmap::DashMap;
|
||||
use log::info;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::utils::parceyaml::Configuration;
|
||||
use crate::utils::structs::Configuration;
|
||||
use axum::body::Body;
|
||||
use axum::extract::State;
|
||||
use axum::http::{Response, StatusCode};
|
||||
|
||||
Reference in New Issue
Block a user