From 2faab82c80d506b764e9f2a68553931ba1da2f69 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sat, 30 Dec 2023 15:49:52 +0300 Subject: [PATCH] Create src/page/mod.rs --- .gitignore | 5 ++++ src/main.rs | 7 ++++- src/page/mod.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/page/mod.rs diff --git a/.gitignore b/.gitignore index 3195e6f..b6d49d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,11 @@ * !src/ +!src/page/ +!src/page/cube/ +!src/routes/ +!src/routes/_404/ +!src/routes/index/ !.gitignore diff --git a/src/main.rs b/src/main.rs index 7ea8418..7b95138 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,8 @@ +#![feature(lazy_cell)] + +mod page; +// mod routes; + use actix_web::{ main as async_main, App, @@ -26,7 +31,7 @@ async fn main() -> anyhow::Result<()> { .target(env_logger::Target::Stdout) .init(); - HttpServer::new(App::new) + HttpServer::new(App::new) //|| App::new().route(routes::index)) .bind(("0.0.0.0", args.port)) .with_context(|| format!("Failed to bind to 0.0.0.0:{port}", port = args.port))? .run() diff --git a/src/page/mod.rs b/src/page/mod.rs new file mode 100644 index 0000000..73d7562 --- /dev/null +++ b/src/page/mod.rs @@ -0,0 +1,79 @@ +use std::sync::LazyLock; + +use anyhow::Context; +use cargo_toml::Manifest; +use maud::{ + html, + Markup, + DOCTYPE, +}; + +static MANIFEST: LazyLock = LazyLock::new(|| { + Manifest::from_str(&embed::string!("../../Cargo.toml")) + .with_context(|| "Failed to deserialize Cargo manifest.") + .unwrap() +}); + +fn property(name: &str, content: &str) -> Markup { + html! { + meta property=(name) content=(content); + } +} + +fn pname(name: &str, content: &str) -> Markup { + html! { + meta name=(name) content=(content); + } +} + +/// Creates an asset URL from the given asset path. +pub(crate) fn asset(path: &str) -> String { + format!("/assets/{path}") +} + +/// Creates a page with the given head and body. +/// +/// This is the most low level function for page creation +/// as all pages use this, as this function provides the +/// page title, OpenGraph and other information. +pub(crate) fn create(head: Markup, body: Markup) -> Markup { + html! { + (DOCTYPE) + + head { + meta charset="UTF-8"; + + (pname("viewport", "width=device-width, initial-scale=1.0")) + (property("og:type", "website")) + + @let name = &MANIFEST.package.as_ref().unwrap().authors()[0]; + + title { (name) } + (pname("author", name)) + + (property("og:site_name", name)) + (property("og:title", name)) + + @let description = MANIFEST.package.as_ref().unwrap().description().unwrap(); + (pname("description", description)) + (property("og:description", description)) + + link rel="icon" href=(asset("icon.gif")) type="image/gif"; + + (property("og:image", &asset("thumbnail.png"))) + (property("og:image:type", "image/png")) + (property("og:image:height", "1080")) + (property("og:image:width", "600")) + + @let url = MANIFEST.package.as_ref().unwrap().homepage().unwrap(); + (property("og:url", url)) + link rel="canonical" href=(url); + + (head) + } + + body { + (body) + } + } +}