From 8d79d7640cadab30ff1e7bfa8df41eb08ffbdf48 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 2 Jan 2024 14:30:44 +0100 Subject: Add more pages, improve nav --- Cargo.lock | 1 - Cargo.toml | 1 - build.rs | 23 +++++++++++-------- src/config.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++--- static/style.css | 2 +- templates/head.html | 31 ++++++++++++++----------- templates/incoming.html | 6 +++++ templates/send.html | 6 +++++ templates/settings.html | 6 +++++ 10 files changed, 166 insertions(+), 28 deletions(-) create mode 100644 src/config.rs create mode 100644 templates/incoming.html create mode 100644 templates/send.html create mode 100644 templates/settings.html diff --git a/Cargo.lock b/Cargo.lock index e3b36ce..27a6889 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -270,7 +270,6 @@ dependencies = [ "axum", "log", "serde", - "serde_json", "simple_logger", "sqlx", "tokio", diff --git a/Cargo.toml b/Cargo.toml index ace615e..91fb20d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ axum = "0.7" simple_logger = "4.3" log = "0.4" serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" toml = "0.8" sqlx = { version = "0.7", features = [ "runtime-tokio-rustls", "sqlite"]} tokio = { version = "1", features = ["full"] } diff --git a/build.rs b/build.rs index fdc9ce7..b754eff 100644 --- a/build.rs +++ b/build.rs @@ -5,15 +5,20 @@ fn main() { let input = format!("{dir}/style.css"); let output = format!("{dir}/static/style.css"); - let result = std::process::Command::new("tailwindcss") + match std::process::Command::new("tailwindcss") .args(["-m", "--input", &input, "--output", &output]) .output() - .expect("Unable to generate css"); - - if !result.status.success() { - let error = String::from_utf8_lossy(&result.stderr); - println!("cargo:warning=tailwind returned {}", result.status); - println!("cargo:warning=Unable to build CSS !"); - println!("cargo:warning=Output: {error}"); - } + { + Ok(result) => { + if !result.status.success() { + let error = String::from_utf8_lossy(&result.stderr); + println!("cargo:warning=tailwindcss returned {}", result.status); + println!("cargo:warning=Unable to build CSS !"); + println!("cargo:warning=Output: {error}"); + } + }, + Err(e) => { + println!("cargo:warning=Could not run tailwindcss: {}", e); + }, + } } diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..51bf507 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,57 @@ +use std::fs; +use anyhow::Context; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Clone)] +pub struct FelinetConfig { + pub enabled: bool, + pub address: String, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct TunnelConfig { + pub enabled: bool, + pub local_ip: String, + pub netmask: String, +} + +type DurationSeconds = std::num::NonZeroU32; + +#[derive(Serialize, Deserialize, Clone)] +pub struct BeaconConfig { + pub period_seconds: Option, + #[serde(default)] + pub max_hops: u8, + pub latitude: Option, + pub longitude: Option, + pub altitude: Option, + pub comment: Option, + pub antenna_height: Option, + pub antenna_gain: Option, + pub tx_power: Option, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct Config { + pub callsign: String, + pub ssid: u8, + #[serde(default)] + pub icon: u16, + pub felinet: FelinetConfig, + pub beacon: BeaconConfig, + pub tunnel: Option, +} + +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") + } + + pub fn store(&self) -> anyhow::Result<()> { + fs::write(CONFIGFILE, toml::to_string_pretty(&self)?) + .context("writing config file") + } +} diff --git a/src/main.rs b/src/main.rs index 1850e66..cab477c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,8 @@ use axum::{ use sqlx::{Connection, SqliteConnection}; use tower_http::services::ServeDir; +mod config; + struct AppState { callsign : String, db : Mutex @@ -20,6 +22,9 @@ type SharedState = Arc; #[tokio::main] async fn main() -> std::io::Result<()> { + + // simple_logger:: + let mut conn = SqliteConnection::connect("sqlite:cats-radio-node.db").await.unwrap(); sqlx::migrate!() .run(&mut conn) @@ -35,6 +40,9 @@ async fn main() -> std::io::Result<()> { let app = Router::new() .route("/", get(dashboard)) + .route("/incoming", get(incoming)) + .route("/send", get(send)) + .route("/settings", get(settings)) .route("/form", get(show_form).post(accept_form)) .nest_service("/static", ServeDir::new("static")) /* requires tracing and tower, e.g. @@ -70,6 +78,7 @@ enum ActivePage { Dashboard, Incoming, Send, + Settings, } #[derive(Template)] @@ -80,9 +89,7 @@ struct DashboardTemplate<'a> { callsign: String, } -async fn dashboard( - State(state): State, - ) -> DashboardTemplate<'static> { +async fn dashboard(State(state): State) -> DashboardTemplate<'static> { DashboardTemplate { title: "Dashboard", callsign: state.callsign.clone(), @@ -90,6 +97,54 @@ async fn dashboard( } } +#[derive(Template)] +#[template(path = "incoming.html")] +struct IncomingTemplate<'a> { + title: &'a str, + page: ActivePage, + callsign: String, +} + +async fn incoming(State(state): State) -> IncomingTemplate<'static> { + IncomingTemplate { + title: "Incoming", + callsign: state.callsign.clone(), + page: ActivePage::Incoming, + } +} + +#[derive(Template)] +#[template(path = "send.html")] +struct SendTemplate<'a> { + title: &'a str, + page: ActivePage, + callsign: String, +} + +async fn send(State(state): State) -> SendTemplate<'static> { + SendTemplate { + title: "Send", + callsign: state.callsign.clone(), + page: ActivePage::Send, + } +} + +#[derive(Template)] +#[template(path = "settings.html")] +struct SettingsTemplate<'a> { + title: &'a str, + page: ActivePage, + callsign: String, +} + +async fn settings(State(state): State) -> SettingsTemplate<'static> { + SettingsTemplate { + title: "Settings", + callsign: state.callsign.clone(), + page: ActivePage::Settings, + } +} + async fn show_form() -> Html<&'static str> { Html( r#" diff --git a/static/style.css b/static/style.css index 0998844..5b5b8df 100644 --- a/static/style.css +++ b/static/style.css @@ -1 +1 @@ -/*! tailwindcss v3.3.5 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border:0 solid #e5e7eb}:after,:before{--tw-content:""}html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;font-feature-settings:normal;font-variation-settings:normal}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-feature-settings:inherit;font-variation-settings:inherit;font-size:100%;font-weight:inherit;line-height:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}dialog{padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: }html{--tw-bg-opacity:1;background-color:rgb(255 255 255/var(--tw-bg-opacity));font-family:sans-serif}.flex{display:flex}.h-full{height:100%}.min-h-screen{min-height:100vh}.w-60{width:15rem}.flex-auto{flex:1 1 auto}.flex-none{flex:none}.items-center{align-items:center}.space-x-3>:not([hidden])~:not([hidden]){--tw-space-x-reverse:0;margin-right:calc(.75rem*var(--tw-space-x-reverse));margin-left:calc(.75rem*(1 - var(--tw-space-x-reverse)))}.space-y-1>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(.25rem*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(.25rem*var(--tw-space-y-reverse))}.space-y-2>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(.5rem*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(.5rem*var(--tw-space-y-reverse))}.divide-y>:not([hidden])~:not([hidden]){--tw-divide-y-reverse:0;border-top-width:calc(1px*(1 - var(--tw-divide-y-reverse)));border-bottom-width:calc(1px*var(--tw-divide-y-reverse))}.divide-gray-300>:not([hidden])~:not([hidden]){--tw-divide-opacity:1;border-color:rgb(209 213 219/var(--tw-divide-opacity))}.rounded-md{border-radius:.375rem}.bg-gray-100{--tw-bg-opacity:1;background-color:rgb(243 244 246/var(--tw-bg-opacity))}.bg-gray-200{--tw-bg-opacity:1;background-color:rgb(229 231 235/var(--tw-bg-opacity))}.p-2{padding:.5rem}.p-3{padding:.75rem}.p-4{padding:1rem}.pb-4{padding-bottom:1rem}.pt-2{padding-top:.5rem}.text-lg{font-size:1.125rem;line-height:1.75rem}.text-sm{font-size:.875rem;line-height:1.25rem}.font-semibold{font-weight:600}.text-gray-800{--tw-text-opacity:1;color:rgb(31 41 55/var(--tw-text-opacity))}.text-gray-900{--tw-text-opacity:1;color:rgb(17 24 39/var(--tw-text-opacity))}h1{font-size:1.125rem;line-height:1.75rem}h1,h2{font-weight:700} \ No newline at end of file +/*! tailwindcss v3.3.5 | MIT License | https://tailwindcss.com*/*,:after,:before{box-sizing:border-box;border:0 solid #e5e7eb}:after,:before{--tw-content:""}html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;font-feature-settings:normal;font-variation-settings:normal}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,pre,samp{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:initial}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-feature-settings:inherit;font-variation-settings:inherit;font-size:100%;font-weight:inherit;line-height:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button;background-color:initial;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:initial}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dd,dl,figure,h1,h2,h3,h4,h5,h6,hr,p,pre{margin:0}fieldset{margin:0}fieldset,legend{padding:0}menu,ol,ul{list-style:none;margin:0;padding:0}dialog{padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#9ca3af}input::placeholder,textarea::placeholder{opacity:1;color:#9ca3af}[role=button],button{cursor:pointer}:disabled{cursor:default}audio,canvas,embed,iframe,img,object,svg,video{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,::backdrop,:after,:before{--tw-border-spacing-x:0;--tw-border-spacing-y:0;--tw-translate-x:0;--tw-translate-y:0;--tw-rotate:0;--tw-skew-x:0;--tw-skew-y:0;--tw-scale-x:1;--tw-scale-y:1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness:proximity;--tw-gradient-from-position: ;--tw-gradient-via-position: ;--tw-gradient-to-position: ;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width:0px;--tw-ring-offset-color:#fff;--tw-ring-color:#3b82f680;--tw-ring-offset-shadow:0 0 #0000;--tw-ring-shadow:0 0 #0000;--tw-shadow:0 0 #0000;--tw-shadow-colored:0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: }html{--tw-bg-opacity:1;background-color:rgb(255 255 255/var(--tw-bg-opacity));font-family:sans-serif}.m-2{margin:.5rem}.flex{display:flex}.h-full{height:100%}.min-h-screen{min-height:100vh}.w-60{width:15rem}.w-8{width:2rem}.flex-auto{flex:1 1 auto}.flex-none{flex:none}.space-y-1>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(.25rem*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(.25rem*var(--tw-space-y-reverse))}.space-y-2>:not([hidden])~:not([hidden]){--tw-space-y-reverse:0;margin-top:calc(.5rem*(1 - var(--tw-space-y-reverse)));margin-bottom:calc(.5rem*var(--tw-space-y-reverse))}.divide-y>:not([hidden])~:not([hidden]){--tw-divide-y-reverse:0;border-top-width:calc(1px*(1 - var(--tw-divide-y-reverse)));border-bottom-width:calc(1px*var(--tw-divide-y-reverse))}.divide-sky-300>:not([hidden])~:not([hidden]){--tw-divide-opacity:1;border-color:rgb(125 211 252/var(--tw-divide-opacity))}.rounded-lg{border-radius:.5rem}.rounded-md{border-radius:.375rem}.bg-sky-100{--tw-bg-opacity:1;background-color:rgb(224 242 254/var(--tw-bg-opacity))}.bg-sky-200{--tw-bg-opacity:1;background-color:rgb(186 230 253/var(--tw-bg-opacity))}.bg-sky-300{--tw-bg-opacity:1;background-color:rgb(125 211 252/var(--tw-bg-opacity))}.p-2{padding:.5rem}.p-3{padding:.75rem}.p-4{padding:1rem}.pb-4{padding-bottom:1rem}.pt-2{padding-top:.5rem}.text-lg{font-size:1.125rem;line-height:1.75rem}.font-semibold{font-weight:600}.text-sky-800{--tw-text-opacity:1;color:rgb(7 89 133/var(--tw-text-opacity))}.text-sky-900{--tw-text-opacity:1;color:rgb(12 74 110/var(--tw-text-opacity))}h1{font-size:1.125rem;line-height:1.75rem}h1,h2{font-weight:700}.hover\:bg-sky-300:hover{--tw-bg-opacity:1;background-color:rgb(125 211 252/var(--tw-bg-opacity))} \ No newline at end of file diff --git a/templates/head.html b/templates/head.html index 0d7d2bc..f15487c 100644 --- a/templates/head.html +++ b/templates/head.html @@ -11,27 +11,32 @@