Refactor config loading

This commit is contained in:
Leonard Steppy 2025-02-14 15:20:56 +01:00
parent 775cb0ca4e
commit 78a4f0225a
10 changed files with 79 additions and 58 deletions

View File

@ -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"}
serde_json = "1.0"

View File

@ -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"]
command_arguments = ["run", "--bin", "create_default_configs"]

View File

@ -1,8 +0,0 @@
[package]
name = "config"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View File

@ -1,6 +0,0 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionConfig {
pub motd: String,
}

View File

@ -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<T, P>(name: &str, out_dir: P, default_config: &T)
where
T: Serialize,
P: AsRef<Path>,
{
//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();
}

View File

@ -7,6 +7,7 @@
color:red;
}
</style>
<link data-trunk rel="rust" data-bin="jana_sessions_webpage" />
</head>
<body></body>
</html>

View File

@ -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::<SessionConfig, _>("session_config", &out_dir)?;
Ok(())
}
fn create_default_config<T, P>(name: &str, out_dir: P) -> io::Result<()>
where
T: Serialize + Default,
P: AsRef<Path>,
{
create_config(name, out_dir, &T::default())
}
fn create_config<T, P>(name: &str, out_dir: P, default_config: &T) -> io::Result<()>
where
T: Serialize,
P: AsRef<Path>,
{
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(())
}

2
src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod session_date_calculator;
pub mod webpage;

View File

@ -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")
)
}
}

View File

@ -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::<SessionConfig>("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! { <Sessions config /> }.into_any(),
Ok(config) => {
let config = config.unwrap_or_default();
view! { <Sessions config /> }.into_any()
}
Err(e) => {
view! { <p class="red">{format!("Failed to load Sessions config: {e}")}</p> }.into_any()
}
@ -36,14 +40,39 @@ fn Sessions(config: SessionConfig) -> impl IntoView {
}
}
async fn load_config() -> Result<SessionConfig, String> {
let response = Request::get("/session_config.json")
async fn load_config<T>(name: &str) -> Result<Option<T>, 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::<SessionConfig>()
.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(),
}
}
}