From 4ba802d0c73a1a1664b4d3e17757e54aeefd81f7 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 2 Jan 2024 15:12:47 +0100 Subject: Add default config, and start adding form --- src/config.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- src/main.rs | 56 +++++++++++-------------------------------------- 2 files changed, 72 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/config.rs b/src/config.rs index 51bf507..33ba361 100644 --- a/src/config.rs +++ b/src/config.rs @@ -2,22 +2,41 @@ use std::fs; use anyhow::Context; use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct FelinetConfig { pub enabled: bool, pub address: String, } -#[derive(Serialize, Deserialize, Clone)] +impl Default for FelinetConfig { + fn default() -> Self { + FelinetConfig { + enabled: false, + address: "https://felinet.cats.radio".to_owned() + } + } +} + +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct TunnelConfig { pub enabled: bool, pub local_ip: String, pub netmask: String, } +impl Default for TunnelConfig { + fn default() -> Self { + TunnelConfig { + enabled: false, + local_ip: "10.73.14.1".to_owned(), + netmask: "255.255.255.0".to_owned(), + } + } +} + type DurationSeconds = std::num::NonZeroU32; -#[derive(Serialize, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct BeaconConfig { pub period_seconds: Option, #[serde(default)] @@ -31,7 +50,23 @@ pub struct BeaconConfig { pub tx_power: Option, } -#[derive(Serialize, Deserialize, Clone)] +impl Default for BeaconConfig { + fn default() -> Self { + BeaconConfig { + period_seconds: None, + max_hops: 3, + latitude: None, + longitude: None, + altitude: None, + comment: None, + antenna_height: None, + antenna_gain: None, + tx_power: None, + } + } +} + +#[derive(Debug, Serialize, Deserialize, Clone)] pub struct Config { pub callsign: String, pub ssid: u8, @@ -39,15 +74,33 @@ pub struct Config { pub icon: u16, pub felinet: FelinetConfig, pub beacon: BeaconConfig, - pub tunnel: Option, + pub tunnel: TunnelConfig, +} + +impl Default for Config { + fn default() -> Self { + Config { + callsign: "CHANGEME".to_owned(), + ssid: 0, + icon: 0, + felinet: Default::default(), + beacon: Default::default(), + tunnel: Default::default(), + } + } } const CONFIGFILE : &str = "node-config.toml"; impl Config { pub fn load() -> anyhow::Result { - let file_contents = fs::read_to_string(CONFIGFILE)?; - toml::from_str(&file_contents).context("parsing config file") + if std::path::Path::new(CONFIGFILE).exists() { + let file_contents = fs::read_to_string(CONFIGFILE)?; + toml::from_str(&file_contents).context("parsing config file") + } + else { + Ok(Default::default()) + } } pub fn store(&self) -> anyhow::Result<()> { diff --git a/src/main.rs b/src/main.rs index cab477c..50baf2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use tower_http::services::ServeDir; mod config; struct AppState { - callsign : String, + conf : config::Config, db : Mutex } @@ -31,10 +31,10 @@ async fn main() -> std::io::Result<()> { .await .expect("could not run SQLx migrations"); - let callsign = "HB9EGM-0".to_owned(); + let conf = config::Config::load().expect("Could not load config"); let shared_state = Arc::new(AppState { - callsign, + conf, db: Mutex::new(conn) }); @@ -42,8 +42,7 @@ async fn main() -> std::io::Result<()> { .route("/", get(dashboard)) .route("/incoming", get(incoming)) .route("/send", get(send)) - .route("/settings", get(settings)) - .route("/form", get(show_form).post(accept_form)) + .route("/settings", get(show_settings).post(post_settings)) .nest_service("/static", ServeDir::new("static")) /* requires tracing and tower, e.g. * tower = { version = "0.4", features = ["util", "timeout"] } @@ -92,7 +91,7 @@ struct DashboardTemplate<'a> { async fn dashboard(State(state): State) -> DashboardTemplate<'static> { DashboardTemplate { title: "Dashboard", - callsign: state.callsign.clone(), + callsign: state.conf.callsign.clone(), page: ActivePage::Dashboard, } } @@ -108,7 +107,7 @@ struct IncomingTemplate<'a> { async fn incoming(State(state): State) -> IncomingTemplate<'static> { IncomingTemplate { title: "Incoming", - callsign: state.callsign.clone(), + callsign: state.conf.callsign.clone(), page: ActivePage::Incoming, } } @@ -124,7 +123,7 @@ struct SendTemplate<'a> { async fn send(State(state): State) -> SendTemplate<'static> { SendTemplate { title: "Send", - callsign: state.callsign.clone(), + callsign: state.conf.callsign.clone(), page: ActivePage::Send, } } @@ -135,49 +134,18 @@ struct SettingsTemplate<'a> { title: &'a str, page: ActivePage, callsign: String, + conf: config::Config, } -async fn settings(State(state): State) -> SettingsTemplate<'static> { +async fn show_settings(State(state): State) -> SettingsTemplate<'static> { SettingsTemplate { title: "Settings", - callsign: state.callsign.clone(), + callsign: state.conf.callsign.clone(), page: ActivePage::Settings, + conf: state.conf.clone(), } } -async fn show_form() -> Html<&'static str> { - Html( - r#" - - - - -
- - - - - -
- - - "#, - ) -} - -#[derive(Deserialize, Debug)] -#[allow(dead_code)] -struct Input { - name: String, - email: String, -} - -async fn accept_form(Form(input): Form) { +async fn post_settings(Form(input): Form) { dbg!(&input); } -- cgit v1.2.3