From 0361cf049e78d746c20e72c687b3b13279622a56 Mon Sep 17 00:00:00 2001 From: Chris Jean-Marie Date: Sun, 6 Mar 2022 03:29:04 +0000 Subject: [PATCH] Add template engine --- Cargo.lock | 100 ++++++++++ Cargo.toml | 3 +- src/main.rs | 36 +++- templates/base.html | 215 +++++++++++++++++++++ templates/dashboard.html | 13 ++ templates/index.html | 393 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 756 insertions(+), 4 deletions(-) create mode 100644 templates/base.html create mode 100644 templates/dashboard.html create mode 100644 templates/index.html diff --git a/Cargo.lock b/Cargo.lock index cafb49f..f2556b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,54 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "askama" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb98f10f371286b177db5eeb9a6e5396609555686a35e1d4f7b9a9c6d8af0139" +dependencies = [ + "askama_derive", + "askama_escape", + "askama_shared", +] + +[[package]] +name = "askama_derive" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87bf87e6e8b47264efa9bde63d6225c6276a52e05e91bf37eaa8afd0032d6b71" +dependencies = [ + "askama_shared", + "proc-macro2", + "syn", +] + +[[package]] +name = "askama_escape" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341" + +[[package]] +name = "askama_shared" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf722b94118a07fcbc6640190f247334027685d4e218b794dbfe17c32bf38ed0" +dependencies = [ + "askama_escape", + "humansize", + "mime", + "mime_guess", + "nom", + "num-traits", + "percent-encoding", + "proc-macro2", + "quote", + "serde", + "syn", + "toml", +] + [[package]] name = "async-lock" version = "2.5.0" @@ -442,6 +490,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" +[[package]] +name = "humansize" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02296996cb8796d7c6e3bc2d9211b7802812d36999a51bb754123ead7d37d026" + [[package]] name = "hyper" version = "0.14.17" @@ -475,6 +529,7 @@ checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" name = "jean-marie" version = "0.1.0" dependencies = [ + "askama", "async-session", "axum", "serde", @@ -548,6 +603,22 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "mime_guess" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "mio" version = "0.8.0" @@ -570,6 +641,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "nom" +version = "7.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109" +dependencies = [ + "memchr", + "minimal-lexical", + "version_check", +] + [[package]] name = "ntapi" version = "0.3.7" @@ -958,6 +1040,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +dependencies = [ + "serde", +] + [[package]] name = "tower" version = "0.4.12" @@ -1081,6 +1172,15 @@ version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + [[package]] name = "unicode-xid" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index 3c811d3..a0543e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ tokio = { version = "1.0", features = ["full"] } tracing = "0.1" tracing-subscriber = { version="0.3", features = ["env-filter"] } uuid = { version = "0.8", features = ["v4", "serde"] } -async-session = "3.0.0" \ No newline at end of file +async-session = "3.0.0" +askama = "0.11" diff --git a/src/main.rs b/src/main.rs index c44cbee..cfd8a03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,20 @@ +use askama::Template; use async_session::{MemoryStore, Session, SessionStore as _}; use axum::{ async_trait, - extract::{Extension, FromRequest, RequestParts, TypedHeader}, + extract::{self, Extension, FromRequest, Path, RequestParts, TypedHeader}, headers::Cookie, http::{ self, header::{HeaderMap, HeaderValue}, StatusCode, }, - response::IntoResponse, + response::{Html, IntoResponse, Response}, routing::{get, post}, Json, Router, }; use serde::{Deserialize, Serialize}; use std::net::SocketAddr; - use uuid::Uuid; const AXUM_SESSION_COOKIE_NAME: &str = "axum_session"; @@ -34,6 +34,7 @@ async fn main() { // build our application with a route let app = Router::new() // `GET /` goes to `root` + .route("/greet/:name", get(greet)) .route("/", get(handler)) .layer(Extension(store)); @@ -157,3 +158,32 @@ impl UserId { Self(Uuid::new_v4()) } } + +async fn greet(extract::Path(name): extract::Path) -> impl IntoResponse { + let template = HelloTemplate { name }; + HtmlTemplate(template) +} + +#[derive(Template)] +#[template(path = "index.html")] +struct HelloTemplate { + name: String, +} + +struct HtmlTemplate(T); + +impl IntoResponse for HtmlTemplate +where + T: Template, +{ + fn into_response(self) -> Response { + match self.0.render() { + Ok(html) => Html(html).into_response(), + Err(err) => ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Failed to render template. Error: {}", err), + ) + .into_response(), + } + } +} diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..bc7c911 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,215 @@ + + + + + + + + + + Jean-Marie Family Website + + + + + + + + + + + + +
+
+
+
+
+
+
+ + + + +
+
+
+
+ +
+
+
+
+ + + + + +
{% block content %}{% endblock content %}
+ +
+
+
+
+
+
+
Contact Us
+

Feel free to keep in touch with us!

+
+
    +
  • 010-020-0860
  • +
  • info@company.com
  • +
  • www.company.com
  • +
+
+
+
+
+
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+
+
+
+
+ + + +
+
+
+
+
+

Copyright © 2020 Breezed Co., Ltd. + + - Design: TemplateMo

+
+
+
+
+ +
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/templates/dashboard.html b/templates/dashboard.html new file mode 100644 index 0000000..df2a1ab --- /dev/null +++ b/templates/dashboard.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} +{% block content %} +
+

This will be a site for sharing of public and private information for the extended Jean-Marie family.

+
+

Web links

+

Fonts

+ +
+{% endblock content %} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..9b6aa16 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,393 @@ +{% extends "base.html" %} +{% block content %} +Hello {{ name }} + +
+
+ +
+
+ +
+

Welcome to the

+
Jean-Marie Family Website
+ Learn More + Get It Now +
+
+
+ + +
+
+ +
+

Welcome to the

+
Jean-Marie Family Website
+ Read More + Take Action +
+
+
+ + +
+
+ +
+

Welcome to the

+
Jean-Marie Family Website
+ Learn More + Get It Now +
+
+
+ +
+
+ + + + +
+
+
+
+
+
+
About Us
+

We work with top brands and startups

+
+
+
+
+ +

Top Notch

+
+
+
+
+ +

Robust

+
+
+
+
+ +

Reliable

+
+
+
+
+ +

Up-to-date

+
+
+ +
+
+
+
+
+

Breezed HTML Template is provided by TemplateMo for absolutely free of charge. You can download, edit and use this for your non-commercial and commercial websites. +

Redistributing this template as a downloadable ZIP file on any template collection website is strictly prohibited. You will need to talk to us if you want to redistribute this template. Thank you. +

This is a Bootstrap v4.3.1 CSS layout. Do you like it? You can feel free to talk to us if you have anything.

+
+
+
+
+
+ + + +
+
+
+
+
+
+ +
+
+

Initial Work

+

Proin euismod sem ut diam ultricies, ut faucibus velit ultricies. Nam eu turpis quam. Duis ac condimentum eros.

+ + Learn More + +
+
+
+
+
+
+ +
+
+

Master Planning

+

Proin euismod sem ut diam ultricies, ut faucibus velit ultricies. Nam eu turpis quam. Duis ac condimentum eros.

+ + Learn More + +
+
+
+
+
+
+ +
+
+

Smooth Execution

+

Proin euismod sem ut diam ultricies, ut faucibus velit ultricies. Nam eu turpis quam. Duis ac condimentum eros.

+ + Learn More + +
+
+
+
+
+
+ + + +
+
+
+
+
+
Subscribe Newsletters
+

Don’t miss this chance!

+
+ +
+
+
+
+ + + + +
+
+
+
+
+
Our Projects
+

Some of our latest projects

+
+
+
    +
  • All
  • +
  • Web Design
  • +
  • Web Development
  • +
  • Graphics
  • +
  • Artworks
  • +
+
+
+
+
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+ +
+
+
+
+
+
+
+
+ + + +
+
+
+
+
+
Digital Team
+

young and talented members

+
+
+
+ +
+
+
+
+ + +{% endblock content %} \ No newline at end of file