Added askama integration with axum

Removed minijinja reference
This commit is contained in:
Chris Jean-Marie 2024-09-23 18:24:28 +00:00
parent 4269d49ddf
commit 04c11a03c8
9 changed files with 57 additions and 39 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
backend/target
backend/db
backend/db/db.sqlite3

52
backend/Cargo.lock generated
View File

@ -122,6 +122,17 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "askama_axum"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a41603f7cdbf5ac4af60760f17253eb6adf6ec5b6f14a7ed830cf687d375f163"
dependencies = [
"askama",
"axum-core",
"http 1.1.0",
]
[[package]]
name = "askama_derive"
version = "0.12.5"
@ -1317,6 +1328,7 @@ name = "jean-marie"
version = "0.1.1"
dependencies = [
"askama",
"askama_axum",
"axum",
"axum-extra",
"axum-server",
@ -1326,7 +1338,6 @@ dependencies = [
"dotenvy",
"headers",
"http 1.1.0",
"minijinja",
"oauth2",
"reqwest 0.12.7",
"serde",
@ -1442,12 +1453,6 @@ version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "memo-map"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38d1115007560874e373613744c6fba374c17688327a71c1476d1a5954cc857b"
[[package]]
name = "mime"
version = "0.3.17"
@ -1464,17 +1469,6 @@ dependencies = [
"unicase",
]
[[package]]
name = "minijinja"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1028b628753a7e1a88fc59c9ba4b02ecc3bc0bd3c7af23df667bc28df9b3310e"
dependencies = [
"memo-map",
"self_cell",
"serde",
]
[[package]]
name = "minimal-lexical"
version = "0.2.1"
@ -1794,9 +1788,9 @@ dependencies = [
[[package]]
name = "pkg-config"
version = "0.3.30"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2"
[[package]]
name = "polyval"
@ -2195,12 +2189,6 @@ dependencies = [
"libc",
]
[[package]]
name = "self_cell"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d369a96f978623eb3dc28807c4852d6cc617fed53da5d3c400feff1ef34a714a"
[[package]]
name = "serde"
version = "1.0.210"
@ -2662,18 +2650,18 @@ dependencies = [
[[package]]
name = "thiserror"
version = "1.0.63"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724"
checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.63"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3"
dependencies = [
"proc-macro2",
"quote",
@ -2854,9 +2842,9 @@ dependencies = [
[[package]]
name = "tower-http"
version = "0.6.0"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41515cc9e193536d93fd0dbbea0c73819c08eca76e0b30909a325c3ec90985bb"
checksum = "8437150ab6bbc8c5f0f519e3d5ed4aa883a83dd4cdd3d1b21f9482936046cb97"
dependencies = [
"async-compression",
"base64 0.22.1",

View File

@ -11,17 +11,17 @@ axum = { version = "0.7.6" }
axum_session = { version = "0.14.2" }
axum-server = { version = "0.7.1" }
axum-extra = { version = "0.9.4", features = ["cookie-private", "typed-header"] }
askama = "0.12.0"
askama_axum = "0.4.0"
headers = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.40", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version="0.3", features = ["env-filter"] }
askama = "0.12"
minijinja = { version = "2", features = ["loader"] }
oauth2 = "4.4"
http = "1.1"
tower-http = { version = "0.6.0", features = ["full"] }
tower-http = { version = "0.6.1", features = ["full"] }
chrono = { version = "0.4.38", features = ["serde"] }
sqlx = { version = "0.8", features = ["sqlite", "runtime-tokio"] }
uuid = { version = "1.10", features = ["v4"] }

Binary file not shown.

Binary file not shown.

View File

@ -58,8 +58,8 @@ impl IntoResponse for AppError {
}
}
impl From<minijinja::Error> for AppError {
fn from(err: minijinja::Error) -> Self {
impl From<askama_axum::Error> for AppError {
fn from(err: askama_axum::Error) -> Self {
AppError::new(format!("Template error: {:#}", err))
}
}

View File

@ -1,5 +1,5 @@
use std::net::SocketAddr;
use askama::Template;
use askama_axum::Template;
use axum::{
middleware, response::{Html, IntoResponse, Response}, routing::{get, get_service}, Extension, Router
};
@ -78,9 +78,13 @@ async fn main() {
// build our application with some routes
let app = Router::new()
//Routes that require authentication
.route("/logout", get(logout))
.route("/profile", get(profile))
.route("/useradmin", get(useradmin))
.route_layer(middleware::from_fn_with_state(app_state.clone(), check_auth))
//Routes that don't require authentication
.nest_service("/assets", ServeDir::new("templates/assets")
.fallback(get_service(ServeDir::new("templates/assets"))))
.route("/", get(index))

View File

@ -1,4 +1,4 @@
use askama::Template;
use askama_axum::Template;
use axum::{response::IntoResponse, Extension};
use http::Request;
@ -22,3 +22,22 @@ pub async fn profile<T>(
let template = ProfileTemplate { logged_in, name};
HtmlTemplate(template)
}
#[derive(Template)]
#[template(path = "useradmin.html")]
struct UserAdminTemplate {
logged_in: bool,
name: String,
}
pub async fn useradmin<T>(
Extension(user_data): Extension<Option<UserData>>,
_request: Request<T>,
) -> impl IntoResponse {
let user_email = user_data.map(|s| s.user_email);
let logged_in = user_email.is_some();
let name = user_email.unwrap_or_default();
let template = UserAdminTemplate { logged_in, name};
HtmlTemplate(template)
}

View File

@ -0,0 +1,6 @@
{% extends "base.html" %}
{% block title %}User Profile{% endblock %}
{% block content %}
This is the user administration page. <br />
Your email address: {{ name }}.
{% endblock %}