Konfiguration auf yaml umgestellt
This commit is contained in:
1 parent
e74689c70f
commit
09c41821c8
7 files changed
+283
-123
No files matched your search
+16
-75
@@ -5,7 +5,7 @@ use std::sync::Mutex;
|
||||
use tera::{Context, Tera};
|
||||
// use config::{Config, File};
|
||||
use std::fs;
|
||||
use toml_edit::{Document, value, Item};
|
||||
use serde_yaml;
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ struct AppState {
|
||||
impl AppState {
|
||||
fn load_from_conf(conf_path: &str) -> Self {
|
||||
let conf_str = std::fs::read_to_string(conf_path).expect("Config-Datei konnte nicht gelesen werden");
|
||||
let config: AppConfig = toml::from_str(&conf_str).expect("Config-Deserialisierung fehlgeschlagen");
|
||||
let config: AppConfig = serde_yaml::from_str(&conf_str).expect("Config-Deserialisierung fehlgeschlagen");
|
||||
|
||||
let tera = match Tera::new("templates/**/*") {
|
||||
Ok(t) => t,
|
||||
@@ -170,34 +170,24 @@ async fn save_table(
|
||||
req: web::Json<SaveTableRequest>,
|
||||
) -> Result<HttpResponse> {
|
||||
let mut config = data.config.lock().unwrap();
|
||||
// Lade bestehende Datei mit toml_edit
|
||||
let conf_path = "old/paramod.conf";
|
||||
let conf_str = match fs::read_to_string(conf_path) {
|
||||
Ok(s) => s,
|
||||
Err(e) => return Ok(HttpResponse::InternalServerError().body(format!("Fehler beim Lesen: {}", e))),
|
||||
};
|
||||
let mut doc = match conf_str.parse::<Document>() {
|
||||
Ok(d) => d,
|
||||
Err(e) => return Ok(HttpResponse::InternalServerError().body(format!("TOML-Parse-Fehler: {}", e))),
|
||||
};
|
||||
|
||||
let conf_path = "paramod.yaml";
|
||||
let key = match req.table_id.as_str() {
|
||||
"modbus_input_register" => "modbus_input_register",
|
||||
"modbus_holding_register" => "modbus_holding_register",
|
||||
"modbus_coils" => "modbus_coils",
|
||||
_ => return Ok(HttpResponse::BadRequest().body("Invalid table_id")),
|
||||
};
|
||||
// Serialisiere nur das relevante Feld als TOML
|
||||
let value_toml = match toml::to_string(&req.rows) {
|
||||
match key {
|
||||
"modbus_input_register" => config.modbus_input_register = Some(req.rows.clone()),
|
||||
"modbus_holding_register" => config.modbus_holding_register = Some(req.rows.clone()),
|
||||
"modbus_coils" => config.modbus_coils = Some(req.rows.clone()),
|
||||
_ => {}
|
||||
}
|
||||
let yaml_str = match serde_yaml::to_string(&*config) {
|
||||
Ok(s) => s,
|
||||
Err(e) => return Ok(HttpResponse::InternalServerError().body(format!("Serialisierungsfehler: {}", e))),
|
||||
};
|
||||
// value_toml ist z.B. "[[...]]", wir brauchen nur den Wert
|
||||
let value_item = value_toml.parse::<Item>().unwrap_or(Item::None);
|
||||
doc[key] = value_item;
|
||||
|
||||
// Schreibe zurück
|
||||
if let Err(e) = fs::write(conf_path, doc.to_string()) {
|
||||
if let Err(e) = fs::write(conf_path, yaml_str) {
|
||||
return Ok(HttpResponse::InternalServerError().body(format!("Fehler beim Schreiben: {}", e)));
|
||||
}
|
||||
Ok(HttpResponse::Ok().body("success"))
|
||||
@@ -209,61 +199,12 @@ async fn save_settings(
|
||||
) -> Result<HttpResponse> {
|
||||
let mut config = data.config.lock().unwrap();
|
||||
*config = settings.into_inner();
|
||||
let conf_path = "old/paramod.conf";
|
||||
let conf_str = match fs::read_to_string(conf_path) {
|
||||
let conf_path = "paramod.yaml";
|
||||
let yaml_str = match serde_yaml::to_string(&*config) {
|
||||
Ok(s) => s,
|
||||
Err(e) => return Ok(HttpResponse::InternalServerError().body(format!("Fehler beim Lesen: {}", e))),
|
||||
Err(e) => return Ok(HttpResponse::InternalServerError().body(format!("Serialisierungsfehler: {}", e))),
|
||||
};
|
||||
let mut doc = match conf_str.parse::<Document>() {
|
||||
Ok(d) => d,
|
||||
Err(e) => return Ok(HttpResponse::InternalServerError().body(format!("TOML-Parse-Fehler: {}", e))),
|
||||
};
|
||||
// mqtt
|
||||
let mqtt_value = toml::Value::try_from(&config.mqtt).map_err(|e| actix_web::error::ErrorInternalServerError(format!("Serialisierungsfehler: {}", e)))?;
|
||||
if let toml::Value::Table(table) = mqtt_value {
|
||||
let mut item = Item::Table(Default::default());
|
||||
if let Item::Table(ref mut t) = item {
|
||||
for (k, v) in table {
|
||||
t[&k] = v.to_string().parse::<Item>().unwrap_or(Item::None);
|
||||
}
|
||||
}
|
||||
doc["mqtt"] = item;
|
||||
}
|
||||
// influxdb
|
||||
let influxdb_value = toml::Value::try_from(&config.influxdb).map_err(|e| actix_web::error::ErrorInternalServerError(format!("Serialisierungsfehler: {}", e)))?;
|
||||
if let toml::Value::Table(table) = influxdb_value {
|
||||
let mut item = Item::Table(Default::default());
|
||||
if let Item::Table(ref mut t) = item {
|
||||
for (k, v) in table {
|
||||
t[&k] = v.to_string().parse::<Item>().unwrap_or(Item::None);
|
||||
}
|
||||
}
|
||||
doc["influxdb"] = item;
|
||||
}
|
||||
// modbus
|
||||
let modbus_value = toml::Value::try_from(&config.modbus).map_err(|e| actix_web::error::ErrorInternalServerError(format!("Serialisierungsfehler: {}", e)))?;
|
||||
if let toml::Value::Table(table) = modbus_value {
|
||||
let mut item = Item::Table(Default::default());
|
||||
if let Item::Table(ref mut t) = item {
|
||||
for (k, v) in table {
|
||||
t[&k] = v.to_string().parse::<Item>().unwrap_or(Item::None);
|
||||
}
|
||||
}
|
||||
doc["modbus"] = item;
|
||||
}
|
||||
// [default] Abschnitt
|
||||
let default_value = toml::Value::try_from(&config.default).map_err(|e| actix_web::error::ErrorInternalServerError(format!("Serialisierungsfehler: {}", e)))?;
|
||||
if let toml::Value::Table(table) = default_value {
|
||||
let mut item = Item::Table(Default::default());
|
||||
if let Item::Table(ref mut t) = item {
|
||||
for (k, v) in table {
|
||||
t[&k] = v.to_string().parse::<Item>().unwrap_or(Item::None);
|
||||
}
|
||||
}
|
||||
doc["default"] = item;
|
||||
}
|
||||
// Schreibe zurück
|
||||
if let Err(e) = fs::write(conf_path, doc.to_string()) {
|
||||
if let Err(e) = fs::write(conf_path, yaml_str) {
|
||||
return Ok(HttpResponse::InternalServerError().body(format!("Fehler beim Schreiben: {}", e)));
|
||||
}
|
||||
Ok(HttpResponse::Ok().body("success"))
|
||||
@@ -272,7 +213,7 @@ async fn save_settings(
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
|
||||
let config_path = "old/paramod.conf";
|
||||
let config_path = "paramod.yaml";
|
||||
let app_state = web::Data::new(AppState::load_from_conf(config_path));
|
||||
|
||||
println!("Server läuft auf http://0.0.0.0:8080");
|
||||
|
||||
Reference in new issue
Block a user