From 04c11a03c8d90d194c71fde3c2b02d00bc55d4b5 Mon Sep 17 00:00:00 2001 From: Chris Jean-Marie Date: Mon, 23 Sep 2024 18:24:28 +0000 Subject: [PATCH] Added askama integration with axum Removed minijinja reference --- .gitignore | 1 + backend/Cargo.lock | 52 ++++++++++++------------------- backend/Cargo.toml | 6 ++-- backend/db/db.sqlite3-shm | Bin 32768 -> 0 bytes backend/db/db.sqlite3-wal | Bin 82432 -> 0 bytes backend/src/error_handling.rs | 4 +-- backend/src/main.rs | 6 +++- backend/src/routes.rs | 21 ++++++++++++- backend/templates/useradmin.html | 6 ++++ 9 files changed, 57 insertions(+), 39 deletions(-) delete mode 100644 backend/db/db.sqlite3-shm delete mode 100644 backend/db/db.sqlite3-wal create mode 100644 backend/templates/useradmin.html diff --git a/.gitignore b/.gitignore index 64aadb7..0a1abd8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ backend/target backend/db +backend/db/db.sqlite3 diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 4bfa366..4e55654 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -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", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 1b0a63e..58cc602 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -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"] } diff --git a/backend/db/db.sqlite3-shm b/backend/db/db.sqlite3-shm deleted file mode 100644 index c2ea7978dc6e21e2f3d8353415e1aab658671878..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 32768 zcmeI)J5EAT5CG6YK|}%h6|b?t-VRq_!VWN@q+tQ9f?ZJ2)3O~mB6H0vY_X6;&Loq$ zxw+r7fT=!Tqr`1yBVsVgdY?J%%-4tR@@=tNJ|C*%?q&P2Z@zD4)tT$_sP8D}e*K7g zI-csmUDk4M$ue%&^|y1mT@WBZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C7 z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N z0t7B5(1>RCSnR}O^kWd?i@n3e1pZ#26%W~UwDcnt1g=fsCOiGMv&(KbdNGXAwSC`z x2@oJafB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfWW^HI00}kARqt$ diff --git a/backend/db/db.sqlite3-wal b/backend/db/db.sqlite3-wal deleted file mode 100644 index 1cb50c0c1147acefb9e13a07ba89861e084832df..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 82432 zcmeI*d#EJYT?geS zd1M(TW-t3Im>#C7u3y!s9@PA(^DX6<&i(2Z`j@A-w%)U~o~pF*?6s%A_}ah!oYhW# zz4E8QSLg2hemC@QkF(mr?q2Za>4U%CI{4pp$)M@F|kj|!Uq2*2K)DAv}6YAKP zf=$|4|M}m3!M9&}Ci3dyQg_`KcxpSpb@2Iv%Kq*BkMF;*_r{*R_t?ePE`H>~n-}I6 zo;ZK)e0KNx?#Fg7?|flhHGV1hFc0*yZstOr zWm$--q~k7y*n-OxX_p_dSSV!nyHgqWyontyn_@f}cM{Xcc+|90Zlxm#sZca}g+8-h zP`J!#7{`0f;%H^JBQhVatfEmZBGt@cXrghg{P#gY%*D~DmAwv7^dfEV?$RM zF60oK`J{>D62=7A>K^hy9B=}@#LmNQSaGV zzFPO3^e|g)O!&%>O#G5y;mD+uwCZwa#9>_WM$;-P<3Wy3bt_pt5iW(6x-P_{;=M~8 zZpHgHCF%<;=VVw}kEJHFQZ~bv68yMWF7#!@yOk-c7Lin|O%jz=x1I@m71NDX@+{GC ziYak9j+woCmpI%PKej0`(F(lQEGn&JFI;J)^s<(kR2-C%W?6>xe0>&I%EDBQ)e8BH zREj#X-%3U;wT(uztZS$v=iVg_p`O^3h*UMfDTs;YR4H-s`M9HvVufNpGS6#Pjjn{O zLxg&2f!rw(W*+aQn{jJ_n_6+AM=Q%#xO(prhfwd`lu*b@sE1odYuuxWRxaWi&2pw_ zL}SQ`hp3n<=E|+9Rqe!k@xDj9@!Alh!XzO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EO&PLhc}8w?5EjxBGADXOE0!l~C0(Y-GrUN1`YxNHqn9 zWQ9;s#uk+gKr6-|p!nKKnbRj7>u@gSzrk~l<7Qw`?1J9qGZ E0Jj8?@c;k- diff --git a/backend/src/error_handling.rs b/backend/src/error_handling.rs index 9731ae3..1bd14c1 100644 --- a/backend/src/error_handling.rs +++ b/backend/src/error_handling.rs @@ -58,8 +58,8 @@ impl IntoResponse for AppError { } } -impl From for AppError { - fn from(err: minijinja::Error) -> Self { +impl From for AppError { + fn from(err: askama_axum::Error) -> Self { AppError::new(format!("Template error: {:#}", err)) } } diff --git a/backend/src/main.rs b/backend/src/main.rs index 718d18e..d5abb1a 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -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)) diff --git a/backend/src/routes.rs b/backend/src/routes.rs index 7286af9..37681b8 100644 --- a/backend/src/routes.rs +++ b/backend/src/routes.rs @@ -1,4 +1,4 @@ -use askama::Template; +use askama_axum::Template; use axum::{response::IntoResponse, Extension}; use http::Request; @@ -21,4 +21,23 @@ pub async fn profile( 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( + Extension(user_data): Extension>, + _request: Request, +) -> 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) } \ No newline at end of file diff --git a/backend/templates/useradmin.html b/backend/templates/useradmin.html new file mode 100644 index 0000000..15e071b --- /dev/null +++ b/backend/templates/useradmin.html @@ -0,0 +1,6 @@ +{% extends "base.html" %} +{% block title %}User Profile{% endblock %} +{% block content %} +This is the user administration page.
+Your email address: {{ name }}. +{% endblock %} \ No newline at end of file