replaced askama with tera, reworked the whole website and added nixos module
This commit is contained in:
@@ -1,19 +1,55 @@
|
||||
use askama::Template;
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
extract::State,
|
||||
response::{Html, IntoResponse},
|
||||
};
|
||||
use serde::Serialize;
|
||||
use tera::Context;
|
||||
|
||||
pub async fn render_homepage() -> impl IntoResponse {
|
||||
#[derive(Template)]
|
||||
#[template(path = "homepage.html")]
|
||||
struct HomePageTemplate<'a> {
|
||||
lang: &'a str,
|
||||
}
|
||||
use crate::{AppState, TEMPLATES};
|
||||
|
||||
let tmpl = HomePageTemplate { lang: "en" };
|
||||
match tmpl.render() {
|
||||
/// Handler for the Home Page
|
||||
pub async fn home_handler(State(state): State<Arc<AppState>>) -> impl IntoResponse {
|
||||
let mut context = Context::new();
|
||||
context.insert("title", &state.app_name);
|
||||
context.insert("server_time", &chrono::Local::now().to_rfc2822());
|
||||
|
||||
match TEMPLATES.render("index.html", &context) {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
Err(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Template render error").into_response(),
|
||||
Err(e) => (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handler for the Showcase Page
|
||||
pub async fn showcase_handler() -> impl IntoResponse {
|
||||
#[derive(Serialize)]
|
||||
struct ShowcaseItem {
|
||||
name: String,
|
||||
description: String,
|
||||
}
|
||||
|
||||
let mut context = Context::new();
|
||||
context.insert("title", "Showcase");
|
||||
|
||||
let items = vec![
|
||||
ShowcaseItem {
|
||||
name: "Project Alpha".into(),
|
||||
description: "A cool Rust project".into(),
|
||||
},
|
||||
ShowcaseItem {
|
||||
name: "Project Beta".into(),
|
||||
description: "An Axum powered API".into(),
|
||||
},
|
||||
ShowcaseItem {
|
||||
name: "Project Gamma".into(),
|
||||
description: "Tera template engine".into(),
|
||||
},
|
||||
];
|
||||
context.insert("items", &items);
|
||||
|
||||
match TEMPLATES.render("showcase.html", &context) {
|
||||
Ok(html) => Html(html).into_response(),
|
||||
Err(e) => (axum::http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user