34 lines
1018 B
Rust
34 lines
1018 B
Rust
use std::sync::{Mutex, Arc};
|
|
use tera::Tera;
|
|
use crate::config::{AppConfig, ModbusValueMaps};
|
|
|
|
pub struct AppState {
|
|
pub config: Arc<Mutex<AppConfig>>,
|
|
pub value_maps: Arc<Mutex<ModbusValueMaps>>,
|
|
pub templates: Tera,
|
|
}
|
|
|
|
impl AppState {
|
|
pub 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 = serde_yaml::from_str(&conf_str).expect("Config-Deserialisierung fehlgeschlagen");
|
|
let config = Arc::new(Mutex::new(config));
|
|
let value_maps = Arc::new(Mutex::new(ModbusValueMaps::from_config(&config.lock().unwrap())));
|
|
|
|
let tera = match Tera::new("templates/**/*") {
|
|
Ok(t) => t,
|
|
Err(e) => {
|
|
println!("Template parsing error: {}", e);
|
|
std::process::exit(1);
|
|
}
|
|
};
|
|
|
|
AppState {
|
|
config,
|
|
value_maps,
|
|
templates: tera,
|
|
}
|
|
}
|
|
}
|