From 4423c2e86179168339d90adbda53d9777e953db5 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Sun, 12 Feb 2023 19:42:39 +0100 Subject: switching to rocket --- src/layout.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/layout.rs (limited to 'src/layout.rs') diff --git a/src/layout.rs b/src/layout.rs new file mode 100644 index 0000000..f1d0564 --- /dev/null +++ b/src/layout.rs @@ -0,0 +1,62 @@ +/* + This file is part of metamuffins website (https://codeberg.org/metamuffin/website) + which is licensed under the GNU Affero General Public License (version 3); see /COPYING. + Copyright (C) 2023 metamuffin +*/ +use markup::Render; +use rocket::{ + http::ContentType, + response::{self, Responder}, + Request, Response, +}; +use std::io::Cursor; + +markup::define! { + Layout(title: String, main: Main) { + @markup::doctype() + html { + head { + title { @title " - " "metamuffin's website" } + } + body { + h1 { "metamuffin's personal website" } + nav { + + } + hr; + @main + hr; + footer { + p { "metamuffin's website; " a[href="https://"] {} } + } + } + } + } +} + +pub type DynLayoutPage<'a> = LayoutPage>; + +pub struct LayoutPage { + pub title: String, + pub content: T, +} + +impl<'r, Main: Render> Responder<'r, 'static> for LayoutPage
{ + fn respond_to(self, _req: &'r Request<'_>) -> response::Result<'static> { + // TODO blocking the event loop here. it seems like there is no other way to + // TODO offload this, since the guard references `req` which has a lifetime. + // TODO therefore we just block. that is fine since the database is somewhat fast. + let mut out = String::new(); + Layout { + main: self.content, + title: self.title, + } + .render(&mut out) + .unwrap(); + + Response::build() + .header(ContentType::HTML) + .streamed_body(Cursor::new(out)) + .ok() + } +} -- cgit v1.2.3-70-g09d2