Generate default configs

This commit is contained in:
Leonard Steppy 2025-02-14 03:20:34 +01:00
parent ce1eb3a0d4
commit da9813a2cf
8 changed files with 55 additions and 13 deletions

View File

@ -5,9 +5,12 @@ 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"] }
gloo-timers = { version = "0.3", features = ["futures"] }
serde_json = "1.0"
config = {path = "config"}

View File

@ -1,4 +1,9 @@
[build]
filehash = false
dist = "target/dist"
public_url = "."
public_url = "."
[[hooks]]
stage = "post_build"
command = "cargo"
command_arguments = ["run", "--bin", "create_static_configs"]

7
config/Cargo.toml Normal file
View File

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

14
config/src/lib.rs Normal file
View File

@ -0,0 +1,14 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
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(),
}
}
}

View File

@ -2,12 +2,12 @@
<html>
<head>
<title>Band Sessions</title>
<link data-trunk rel="copy-file" href="session_config.json"/>
<style>
.red {
color:red;
}
</style>
<link data-trunk rel="rust" data-bin="jana_sessions_webpage" />
</head>
<body></body>
</html>

View File

@ -1,3 +0,0 @@
{
"motd": "Proben Sonntags um 10:00 Uhr und Dienstags um 18:30 Uhr"
}

View File

@ -0,0 +1,21 @@
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").expect("Not called during trunk build lifecycle");
create_default_config::<SessionConfig, _>("session_config", &out_dir);
}
fn create_default_config<T, P>(name: &str, out_dir: P)
where
T: Default + Serialize,
P: AsRef<Path>,
{
let out_path = out_dir.as_ref().join(format!("{name}.json"));
let out_file = File::create(&out_path).unwrap();
let default_config = T::default();
serde_json::to_writer_pretty(out_file, &default_config).unwrap();
}

View File

@ -1,11 +1,6 @@
use config::SessionConfig;
use leptos::prelude::*;
use leptos::server_fn::request::browser::Request;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionConfig {
pub motd: String,
}
#[component]
pub fn App() -> impl IntoView {
@ -17,7 +12,7 @@ pub fn App() -> impl IntoView {
.as_deref()
.cloned()
.map(|config| match config {
Ok(config) => view! { <Sessions config></Sessions> }.into_any(),
Ok(config) => view! { <Sessions config /> }.into_any(),
Err(e) => {
view! { <p class="red">{format!("Failed to load Sessions config: {e}")}</p> }.into_any()
}