conf server reestructure

This commit is contained in:
2022-11-30 21:06:19 +01:00
parent ca43b2d75c
commit d992981f22
4 changed files with 112 additions and 61 deletions

View File

@@ -1,4 +1,3 @@
use yaml_rust::yaml;
use serde::{Serialize, Deserialize};
use std::fs::File;
use std::str::FromStr;
@@ -8,9 +7,21 @@ pub mod server_conf;
static FILE: &str = "mrprox.conf";
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct ConfFile{
port: String,
port_conf: String,
servers: Vec<ServerData>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct ServerData{
domain: String,
ip: String,
}
pub struct Config{
l_servers : HashMap<String, String>,
file: Vec<yaml::Yaml>,
port: String,
port_conf: String,
}
@@ -20,40 +31,40 @@ impl Config {
let mut file = File::open(&FILE).unwrap();
let mut s = String::new();
file.read_to_string(&mut s).unwrap();
let yam = yaml::YamlLoader::load_from_str(&s).unwrap();
let yam: ConfFile = serde_yaml::from_str(&s).unwrap();
Self{
l_servers: Self::get_servers(&yam),
port: Self::get_port_f(&yam),
port_conf: Self::get_conf_port_f(&yam),
file: yam,
l_servers: Self::get_servers(&yam.servers),
port: yam.port,
port_conf: yam.port_conf,
}
}
fn get_port_f(file: &Vec<yaml::Yaml>) -> String {
match file[0]["port"].as_str() {
Some(h) => String::from(h),
_ => String::from("25565"),
pub fn add(&mut self, server: ServerData){
self.l_servers.insert(server.domain, server.ip);
}
pub fn del(&mut self, domain: String) -> bool {
match self.l_servers.remove(&domain) {
Some(_s) => true,
None => false,
}
}
fn get_conf_port_f(file: &Vec<yaml::Yaml>) -> String {
match file[0]["port_conf"].as_str() {
Some(h) => String::from(h),
_ => String::from("25565"),
}
}
fn get_servers(file: &Vec<yaml::Yaml>) -> HashMap<String, String> {
let mut ret = HashMap::new();
let docs2 = match file[0]["servers"] {
yaml::Yaml::Hash(ref h) => h,
_ => return ret,
pub fn flush(&self){
let conf = ConfFile {
port: self.port.clone(),
port_conf: self.port_conf.clone(),
servers: Vec::from_iter(self.l_servers.iter()
.map(|(x, y)| ServerData{domain: (*x).clone(), ip: (*y).clone()})),
};
println!("{}", serde_yaml::to_string(&conf).unwrap());
}
for (k, v) in docs2{
ret.insert(String::from(k.as_str().unwrap()),
String::from(v.as_str().unwrap()));
fn get_servers(file: &Vec<ServerData>) -> HashMap<String, String> {
let mut ret = HashMap::new();
for j in file{
ret.insert(j.domain.clone(),j.ip.clone());
}
ret
}
@@ -88,3 +99,4 @@ impl Config {
&self.port_conf
}
}