Bootstrap template engine
This commit is contained in:
@@ -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
1
src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod templates;
|
8
src/templates.rs
Normal file
8
src/templates.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use askama::{
|
||||
Template,
|
||||
};
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
pub struct IndexTemplate {
|
||||
}
|
Reference in New Issue
Block a user