move to maud for templating

This commit is contained in:
insects 2025-02-04 14:40:55 +01:00
parent 5dc1b3d1ff
commit 3584ecce96
7 changed files with 116 additions and 214 deletions

47
src/data.rs Normal file
View file

@ -0,0 +1,47 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
const DATA: &'static str = include_str!("../data.json");
#[derive(Serialize, Deserialize, Debug)]
pub struct Data {
pub db_data: SubData,
pub fish_entries: Vec<FishMeta>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct SubData {
#[serde(alias = "FISH")]
pub fish: HashMap<u32, FishEntry>,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all(deserialize = "camelCase"))]
pub struct FishEntry {
#[serde(alias = "_id")]
pub id: u32,
pub start_hour: Option<f32>,
pub end_hour: Option<f32>,
pub location: Option<u32>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Location {
pub id: u32,
pub name_en: String,
pub map_coords: (f32, f32, u32),
}
#[derive(Serialize, Deserialize, Debug)]
pub struct FishMeta {
pub id: u32,
pub name_en: String,
}
impl Data {
pub fn new() -> Self {
let json = serde_json::from_str(DATA).unwrap();
json
}
}

View file

@ -1,17 +1,13 @@
use std::{env, path::PathBuf, sync::Arc};
use std::sync::Arc;
use axum::{
extract::State,
http::StatusCode,
response::{Html, IntoResponse},
routing::get,
Router,
};
use minijinja::{context, path_loader, Environment};
use minijinja_autoreload::AutoReloader;
use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Router};
use data::Data;
use maud::{html, Markup};
pub mod data;
pub struct AppState {
pub templates: AutoReloader,
pub data: Data,
}
pub struct AppError(anyhow::Error);
@ -36,32 +32,22 @@ where
}
#[axum::debug_handler]
async fn main_handler(state: State<Arc<AppState>>) -> Result<Html<String>, AppError> {
let templates = state.templates.acquire_env()?;
let template = templates.get_template("index.html")?;
let html = template.render(context! {})?;
Ok(Html(html))
async fn main_handler(state: State<Arc<AppState>>) -> Result<Markup, AppError> {
println!("{:?}", state.data.db_data.fish);
let values = state.data.db_data.fish.values();
Ok(html! {
h1 { "Hello!" }
@for fish in values {
li { (fish.id) }
}
})
}
#[tokio::main]
async fn main() {
let disable_autoreload = env::var("DISABLE_AUTORELOAD").as_deref() == Ok("1");
let reloader = AutoReloader::new(move |notifier| {
let template_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("templates");
let mut tmp = Environment::new();
tmp.set_loader(path_loader(&template_path));
notifier.set_fast_reload(true);
if !disable_autoreload {
notifier.watch_path(&template_path, true);
}
Ok(tmp)
});
let app = Router::new()
.route("/", get(main_handler))
.with_state(Arc::new(AppState {
templates: reloader,
}));
.with_state(Arc::new(AppState { data: Data::new() }));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
println!("Listening on http://localhost:3000!");
axum::serve(listener, app).await.unwrap();