CONFIG_PATH für yaml Datei
This commit is contained in:
parent
89f02422e7
commit
9d33984c97
19
src/main.rs
19
src/main.rs
@ -16,6 +16,10 @@ use crate::config::{AppConfig, ModbusValueMaps};
|
||||
use crate::modbus_types::{ModbusInputRegisterConfig, ModbusHoldingRegisterConfig, ModbusCoilsConfig};
|
||||
use crate::app_state::AppState;
|
||||
|
||||
fn get_config_path() -> String {
|
||||
std::env::var("CONFIG_PATH").unwrap_or_else(|_| "paramod.yaml".to_string())
|
||||
}
|
||||
|
||||
async fn index(data: web::Data<AppState>) -> Result<HttpResponse> {
|
||||
let config = data.config.lock().unwrap();
|
||||
let value_maps = data.value_maps.lock().unwrap();
|
||||
@ -138,7 +142,7 @@ async fn save_table(
|
||||
req: web::Json<SaveTableRequest>,
|
||||
) -> Result<HttpResponse> {
|
||||
let mut config = data.config.lock().unwrap();
|
||||
let conf_path = "paramod.yaml";
|
||||
let conf_path = get_config_path();
|
||||
match req.table_id.as_str() {
|
||||
"modbus_input_register" => {
|
||||
let rows: Vec<HashMap<String, ModbusInputRegisterConfig>> =
|
||||
@ -170,7 +174,7 @@ async fn save_table(
|
||||
Ok(s) => s,
|
||||
Err(e) => return Ok(HttpResponse::InternalServerError().body(format!("Serialisierungsfehler: {}", e))),
|
||||
};
|
||||
if let Err(e) = fs::write(conf_path, yaml_str) {
|
||||
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"))
|
||||
@ -195,16 +199,16 @@ async fn save_settings(
|
||||
// Value-Maps neu initialisieren
|
||||
let mut value_maps = data.value_maps.lock().unwrap();
|
||||
*value_maps = ModbusValueMaps::from_config(&new_config);
|
||||
let conf_path = "paramod.yaml";
|
||||
let conf_path = get_config_path();
|
||||
let yaml_str = match serde_yaml::to_string(&new_config) {
|
||||
Ok(s) => s,
|
||||
Err(e) => return Ok(HttpResponse::InternalServerError().body(format!("Serialisierungsfehler: {}", e))),
|
||||
};
|
||||
if let Err(e) = fs::write(conf_path, yaml_str) {
|
||||
if let Err(e) = fs::write(&conf_path, yaml_str) {
|
||||
return Ok(HttpResponse::InternalServerError().body(format!("Fehler beim Schreiben: {}", e)));
|
||||
}
|
||||
// Reload config from file
|
||||
let conf_str = match std::fs::read_to_string(conf_path) {
|
||||
let conf_str = match std::fs::read_to_string(&conf_path) {
|
||||
Ok(s) => s,
|
||||
Err(e) => return Ok(HttpResponse::InternalServerError().body(format!("Fehler beim Lesen: {}", e))),
|
||||
};
|
||||
@ -226,9 +230,8 @@ async fn get_config(data: web::Data<AppState>) -> HttpResponse {
|
||||
#[actix_web::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
|
||||
|
||||
let config_path = "paramod.yaml";
|
||||
let app_state = web::Data::new(AppState::load_from_conf(config_path));
|
||||
let config_path = get_config_path();
|
||||
let app_state = web::Data::new(AppState::load_from_conf(&config_path));
|
||||
|
||||
// Starte Modbus-Polling-Thread
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user