aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2024-09-20 21:24:32 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2024-09-20 21:24:32 +0200
commitad30bb7aedf6b2e959a0ed90e90e2b991a69f6dc (patch)
tree72ade1a402d3e2e53cf9d976da3ca9a2b66b9a3b /src
parent3189dc23f2abf2060f591219e8256301e7c41aed (diff)
downloadodr-dabmux-gui-ad30bb7aedf6b2e959a0ed90e90e2b991a69f6dc.tar.gz
odr-dabmux-gui-ad30bb7aedf6b2e959a0ed90e90e2b991a69f6dc.tar.bz2
odr-dabmux-gui-ad30bb7aedf6b2e959a0ed90e90e2b991a69f6dc.zip
Write dabmux config, add README
Diffstat (limited to 'src')
-rw-r--r--src/config.rs18
-rw-r--r--src/ui.rs51
2 files changed, 24 insertions, 45 deletions
diff --git a/src/config.rs b/src/config.rs
index 64f3c09..68b5f44 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,5 +1,6 @@
use std::{collections::HashMap, fs};
use anyhow::Context;
+use log::error;
use serde::{Deserialize, Serialize};
use serde_json::json;
@@ -54,6 +55,7 @@ impl Service {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
pub instance_name: String,
+ pub dabmux_config_location: String,
pub tist: bool,
pub tist_offset: i32,
// TODO tai_clock_bulletins
@@ -79,6 +81,7 @@ impl Default for Config {
fn default() -> Self {
Config {
instance_name: "CHANGEME".to_owned(),
+ dabmux_config_location: "/etc/odr-dabmux.json".to_owned(),
tist: true,
tist_offset: 0,
ensemble_id: 0x4FFF,
@@ -108,7 +111,11 @@ impl Config {
pub fn load() -> anyhow::Result<Self> {
if std::path::Path::new(CONFIGFILE).exists() {
let file_contents = fs::read_to_string(CONFIGFILE)?;
- toml::from_str(&file_contents).context("parsing config file")
+ toml::from_str(&file_contents)
+ .or_else(|e| {
+ error!("Failed to read existing config file: {}", e);
+ Ok(Default::default())
+ })
}
else {
Ok(Default::default())
@@ -120,7 +127,7 @@ impl Config {
.context("writing config file")
}
- pub fn dump_to_json(&self) -> serde_json::Value {
+ pub fn write_dabmux_json(&self) -> anyhow::Result<()> {
let now = chrono::Utc::now().to_rfc3339();
let mut services = HashMap::new();
@@ -150,7 +157,7 @@ impl Config {
}));
}
- json!({
+ let new_conf = json!({
"_comment": format!("Generated at {} by odr-dabmux-gui", now),
"general": {
"dabmode": 1,
@@ -186,6 +193,9 @@ impl Config {
}
}
}
- })
+ });
+
+ fs::write(&self.dabmux_config_location, serde_json::to_string_pretty(&new_conf)?)
+ .context("writing dabmux config file")
}
}
diff --git a/src/ui.rs b/src/ui.rs
index a5be6a5..4a73ba4 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -9,7 +9,6 @@ use axum::{
};
use serde::Deserialize;
-use log::info;
use tower_http::services::ServeDir;
use crate::config;
@@ -35,7 +34,6 @@ pub async fn serve(port: u16, shared_state: SharedState) {
enum ActivePage {
Dashboard,
Settings,
- None,
}
impl ActivePage {
@@ -44,7 +42,6 @@ impl ActivePage {
match self {
ActivePage::Dashboard => vec!["dashboard.js", "main.js"],
ActivePage::Settings => vec!["settings.js", "main.js"],
- ActivePage::None => vec![],
}
}
}
@@ -95,7 +92,7 @@ struct SetRc {
async fn post_rc(
State(state): State<SharedState>,
- Json(set_rc): Json<SetRc>) -> (StatusCode, Json<serde_json::Value>) {
+ Json(set_rc): Json<SetRc>) -> (StatusCode, String) {
let set_rc_result = {
let mut st = state.lock().unwrap();
@@ -103,11 +100,8 @@ async fn post_rc(
};
match set_rc_result {
- Ok(v) => (StatusCode::OK, Json(v)),
- Err(e) => {
- let e_str = serde_json::Value::String(e.to_string());
- (StatusCode::BAD_REQUEST, Json(e_str))
- },
+ Ok(v) => (StatusCode::OK, v.as_str().or(Some("")).unwrap().to_owned()),
+ Err(e) => (StatusCode::BAD_REQUEST, e.to_string()),
}
}
@@ -127,45 +121,20 @@ async fn show_settings(State(state): State<SharedState>) -> SettingsTemplate<'st
}
}
-#[derive(Template)]
-#[template(path = "settings_applied.html")]
-struct SettingsAppliedTemplate<'a> {
- title: &'a str,
- page: ActivePage,
- conf: config::Config,
- ok: bool,
- error_message: &'a str,
- error_reason: String,
-}
-
async fn post_settings(
State(state): State<SharedState>,
- Json(conf): Json<config::Config>) -> (StatusCode, SettingsAppliedTemplate<'static>) {
+ Json(conf): Json<config::Config>) -> (StatusCode, String) {
match conf.store() {
Ok(()) => {
state.lock().unwrap().conf.clone_from(&conf);
- info!("{}", conf.dump_to_json());
-
- (StatusCode::OK, SettingsAppliedTemplate {
- title: "Settings",
- conf,
- page: ActivePage::None,
- ok: true,
- error_message: "",
- error_reason: "".to_owned(),
- })
+ match conf.write_dabmux_json() {
+ Ok(()) => (StatusCode::OK, "".to_owned()),
+ Err(e) => (StatusCode::INTERNAL_SERVER_ERROR,
+ format!("Failed to write odr-dabmux config: {}", e.to_string()))
+ }
}
- Err(e) => {
- (StatusCode::INTERNAL_SERVER_ERROR, SettingsAppliedTemplate {
- title: "Settings",
- conf,
- page: ActivePage::None,
- ok: false,
- error_message: "Failed to store config",
- error_reason: e.to_string(),
- })
- },
+ Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("Failed to write UI config: {}", e.to_string()))
}
}