Bootstrap template engine

This commit is contained in:
2025-02-01 14:22:33 +01:00
parent e707abbd65
commit b2ea0c311d
6 changed files with 126 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
use anyhow::{
Context,
};
use askama::Template;
use axum::{
extract::{
State,
@@ -17,6 +18,11 @@ use axum::{
get,
},
};
use flake_tracker::{
templates::{
IndexTemplate,
},
};
use sqlx::{
FromRow,
SqlitePool,
@@ -63,6 +69,18 @@ impl<E> From<E> for AppError where E: Into<anyhow::Error> {
}
}
fn render_template<T: Template>(t: &T) -> anyhow::Result<Response> {
let body = t.render()
.context("Failed to render template")?;
Ok((
[
("Content-Type", T::MIME_TYPE),
],
body,
).into_response())
}
#[derive(Clone)]
struct AppState {
db: SqlitePool,
@@ -97,8 +115,8 @@ async fn main() -> anyhow::Result<()> {
async fn route_index(
State(state): State<AppState>,
) -> String {
String::from("Hello world")
) -> Result<impl IntoResponse, AppError> {
Ok(render_template(&IndexTemplate {})?)
}
async fn route_flakes(

1
src/lib.rs Normal file
View File

@@ -0,0 +1 @@
pub mod templates;

8
src/templates.rs Normal file
View File

@@ -0,0 +1,8 @@
use askama::{
Template,
};
#[derive(Template)]
#[template(path = "index.html")]
pub struct IndexTemplate {
}