Refactor config loading
This commit is contained in:
parent
775cb0ca4e
commit
78a4f0225a
@ -5,10 +5,11 @@ edition = "2021"
|
|||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
default-run = "jana_sessions_webpage"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = { version = "0.4", features = ["serde"] }
|
chrono = { version = "0.4", features = ["serde"] }
|
||||||
leptos = { version = "0.7", features = ["csr"] }
|
leptos = { version = "0.7", features = ["csr"] }
|
||||||
console_error_panic_hook = "0.1.7"
|
console_error_panic_hook = "0.1.7"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
config = {path = "config"}
|
|
||||||
@ -4,6 +4,6 @@ dist = "target/dist"
|
|||||||
public_url = "."
|
public_url = "."
|
||||||
|
|
||||||
[[hooks]]
|
[[hooks]]
|
||||||
stage = "post_build"
|
stage = "build"
|
||||||
command = "cargo"
|
command = "cargo"
|
||||||
command_arguments = ["run", "--manifest-path", "config/Cargo.toml"]
|
command_arguments = ["run", "--bin", "create_default_configs"]
|
||||||
@ -1,8 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "config"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
|
||||||
serde_json = "1.0"
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
pub struct SessionConfig {
|
|
||||||
pub motd: String,
|
|
||||||
}
|
|
||||||
@ -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();
|
|
||||||
}
|
|
||||||
@ -7,6 +7,7 @@
|
|||||||
color:red;
|
color:red;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
<link data-trunk rel="rust" data-bin="jana_sessions_webpage" />
|
||||||
</head>
|
</head>
|
||||||
<body></body>
|
<body></body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
32
src/bin/create_default_configs.rs
Normal file
32
src/bin/create_default_configs.rs
Normal 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
2
src/lib.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod session_date_calculator;
|
||||||
|
pub mod webpage;
|
||||||
@ -1,8 +1,6 @@
|
|||||||
mod webpage;
|
|
||||||
mod session_date_calculator;
|
|
||||||
|
|
||||||
use crate::session_date_calculator::Day;
|
|
||||||
use chrono::Weekday;
|
use chrono::Weekday;
|
||||||
|
use jana_sessions_webpage::session_date_calculator::Day;
|
||||||
|
use jana_sessions_webpage::webpage;
|
||||||
use leptos::prelude::*;
|
use leptos::prelude::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -25,4 +23,4 @@ pub fn localize_day(day: &Day) -> String {
|
|||||||
},
|
},
|
||||||
day.date.format("%d.%m.%Y")
|
day.date.format("%d.%m.%Y")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,11 @@
|
|||||||
use config::SessionConfig;
|
|
||||||
use leptos::prelude::*;
|
use leptos::prelude::*;
|
||||||
use leptos::server_fn::request::browser::Request;
|
use leptos::server_fn::request::browser::Request;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn App() -> impl IntoView {
|
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 || {
|
let session_dates = move || {
|
||||||
session_config
|
session_config
|
||||||
@ -12,7 +13,10 @@ pub fn App() -> impl IntoView {
|
|||||||
.as_deref()
|
.as_deref()
|
||||||
.cloned()
|
.cloned()
|
||||||
.map(|config| match config {
|
.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) => {
|
Err(e) => {
|
||||||
view! { <p class="red">{format!("Failed to load Sessions config: {e}")}</p> }.into_any()
|
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> {
|
async fn load_config<T>(name: &str) -> Result<Option<T>, String>
|
||||||
let response = Request::get("/session_config.json")
|
where
|
||||||
|
T: for<'a> Deserialize<'a>,
|
||||||
|
{
|
||||||
|
let response = Request::get(&format!("/{name}.json"))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("HTTP error: {e}"))?;
|
.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
|
let config = response
|
||||||
.json::<SessionConfig>()
|
.json()
|
||||||
.await
|
.await
|
||||||
.map_err(|e| format!("JSON error: {e}"))?;
|
.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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user