diff --git a/Cargo.toml b/Cargo.toml index 8ae639d..e09de2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,10 +5,11 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +default-run = "jana_sessions_webpage" + [dependencies] chrono = { version = "0.4", features = ["serde"] } leptos = { version = "0.7", features = ["csr"] } console_error_panic_hook = "0.1.7" serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" -config = {path = "config"} \ No newline at end of file +serde_json = "1.0" \ No newline at end of file diff --git a/Trunk.toml b/Trunk.toml index 9648c93..a107850 100644 --- a/Trunk.toml +++ b/Trunk.toml @@ -4,6 +4,6 @@ dist = "target/dist" public_url = "." [[hooks]] -stage = "post_build" +stage = "build" command = "cargo" -command_arguments = ["run", "--manifest-path", "config/Cargo.toml"] \ No newline at end of file +command_arguments = ["run", "--bin", "create_default_configs"] \ No newline at end of file diff --git a/config/Cargo.toml b/config/Cargo.toml deleted file mode 100644 index 2c4664b..0000000 --- a/config/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "config" -version = "0.1.0" -edition = "2021" - -[dependencies] -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" diff --git a/config/src/lib.rs b/config/src/lib.rs deleted file mode 100644 index bfcb4b0..0000000 --- a/config/src/lib.rs +++ /dev/null @@ -1,6 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct SessionConfig { - pub motd: String, -} \ No newline at end of file diff --git a/config/src/main.rs b/config/src/main.rs deleted file mode 100644 index 0345cef..0000000 --- a/config/src/main.rs +++ /dev/null @@ -1,28 +0,0 @@ -use config::SessionConfig; -use serde::Serialize; -use std::env; -use std::fs::File; -use std::path::Path; - -fn main() { - let out_dir = env::var_os("TRUNK_STAGING_DIR").unwrap_or("target".into()); - - create_config( - "session_config", - &out_dir, - &SessionConfig { - motd: "Proben Dienstags um 18:30 Uhr und Sonntags um 10:00 Uhr".to_string(), - }, - ) -} - -fn create_config(name: &str, out_dir: P, default_config: &T) -where - T: Serialize, - P: AsRef, -{ - //TODO return, if config already exists and is up to date - let out_path = out_dir.as_ref().join(format!("{name}.json")); - let out_file = File::create(&out_path).unwrap(); - serde_json::to_writer_pretty(out_file, default_config).unwrap(); -} diff --git a/index.html b/index.html index b31a2aa..25b06b2 100644 --- a/index.html +++ b/index.html @@ -7,6 +7,7 @@ color:red; } + diff --git a/src/bin/create_default_configs.rs b/src/bin/create_default_configs.rs new file mode 100644 index 0000000..dff3530 --- /dev/null +++ b/src/bin/create_default_configs.rs @@ -0,0 +1,32 @@ +use jana_sessions_webpage::webpage::SessionConfig; +use serde::Serialize; +use std::fs::File; +use std::path::Path; +use std::{env, fs, io}; + +fn main() -> io::Result<()> { + let out_dir = env::var_os("TRUNK_STAGING_DIR").unwrap_or("target/default_configs".into()); + fs::create_dir_all(&out_dir)?; + + create_default_config::("session_config", &out_dir)?; + Ok(()) +} + +fn create_default_config(name: &str, out_dir: P) -> io::Result<()> +where + T: Serialize + Default, + P: AsRef, +{ + create_config(name, out_dir, &T::default()) +} + +fn create_config(name: &str, out_dir: P, default_config: &T) -> io::Result<()> +where + T: Serialize, + P: AsRef, +{ + let out_path = out_dir.as_ref().join(format!("default_{name}.json")); + let out_file = File::create(&out_path)?; + serde_json::to_writer_pretty(out_file, default_config)?; + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..2cc29d2 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +pub mod session_date_calculator; +pub mod webpage; diff --git a/src/main.rs b/src/main.rs index 95424e9..8a41b71 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,6 @@ -mod webpage; -mod session_date_calculator; - -use crate::session_date_calculator::Day; use chrono::Weekday; +use jana_sessions_webpage::session_date_calculator::Day; +use jana_sessions_webpage::webpage; use leptos::prelude::*; fn main() { @@ -25,4 +23,4 @@ pub fn localize_day(day: &Day) -> String { }, day.date.format("%d.%m.%Y") ) -} \ No newline at end of file +} diff --git a/src/webpage.rs b/src/webpage.rs index 9cea227..14e6d36 100644 --- a/src/webpage.rs +++ b/src/webpage.rs @@ -1,10 +1,11 @@ -use config::SessionConfig; use leptos::prelude::*; use leptos::server_fn::request::browser::Request; +use serde::{Deserialize, Serialize}; #[component] pub fn App() -> impl IntoView { - let session_config = LocalResource::new(load_config); + let session_config = + LocalResource::new(|| async { load_config::("session_config").await }); let session_dates = move || { session_config @@ -12,7 +13,10 @@ pub fn App() -> impl IntoView { .as_deref() .cloned() .map(|config| match config { - Ok(config) => view! { }.into_any(), + Ok(config) => { + let config = config.unwrap_or_default(); + view! { }.into_any() + } Err(e) => { view! {

{format!("Failed to load Sessions config: {e}")}

}.into_any() } @@ -36,14 +40,39 @@ fn Sessions(config: SessionConfig) -> impl IntoView { } } -async fn load_config() -> Result { - let response = Request::get("/session_config.json") +async fn load_config(name: &str) -> Result, String> +where + T: for<'a> Deserialize<'a>, +{ + let response = Request::get(&format!("/{name}.json")) .send() .await .map_err(|e| format!("HTTP error: {e}"))?; + if response + .headers() + .get("content-type") + .is_none_or(|content_type| content_type != "application/json") + { + return Ok(None); + } + let config = response - .json::() + .json() .await .map_err(|e| format!("JSON error: {e}"))?; - Ok(config) + Ok(Some(config)) +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(default)] +pub struct SessionConfig { + pub motd: String, +} + +impl Default for SessionConfig { + fn default() -> Self { + Self { + motd: "Proben Dienstags um 18:30 Uhr und Sonntags um 10:00 Uhr".to_string(), + } + } }