diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 67 | ||||
-rw-r--r-- | src/main.rs | 56 |
2 files changed, 72 insertions, 51 deletions
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<DurationSeconds>, #[serde(default)] @@ -31,7 +50,23 @@ pub struct BeaconConfig { pub tx_power: Option<f32>, } -#[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<TunnelConfig>, + 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<Self> { - 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<SqliteConnection> } @@ -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<SharedState>) -> 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<SharedState>) -> 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<SharedState>) -> 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<SharedState>) -> SettingsTemplate<'static> { +async fn show_settings(State(state): State<SharedState>) -> 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#" - <!doctype html> - <html> - <head></head> - <body> - <form action="/" method="post"> - <label for="name"> - Enter your name: - <input type="text" name="name"> - </label> - - <label> - Enter your email: - <input type="text" name="email"> - </label> - - <input type="submit" value="Subscribe!"> - </form> - </body> - </html> - "#, - ) -} - -#[derive(Deserialize, Debug)] -#[allow(dead_code)] -struct Input { - name: String, - email: String, -} - -async fn accept_form(Form(input): Form<Input>) { +async fn post_settings(Form(input): Form<config::Config>) { dbg!(&input); } |