From 98dc77d7ba59e5a4b4fd26d9404ec8f3572effbb Mon Sep 17 00:00:00 2001 From: lemonsh Date: Fri, 8 Apr 2022 18:54:08 +0200 Subject: [PATCH] Templating works somehow --- src/config.rs | 2 +- src/database.rs | 2 +- src/main.rs | 2 +- src/web.rs | 23 ++++++++++++++++------- templates/base.html | 10 ++++++++++ templates/hello.html | 5 +++++ 6 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 templates/base.html create mode 100644 templates/hello.html diff --git a/src/config.rs b/src/config.rs index 97cff36..619a4bc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ /* * tmtd - Suckless To Do list - * Copyright (C) 2022 lemon.sh & famfo + * Copyright (C) 2022 lemon.sh & karx * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free diff --git a/src/database.rs b/src/database.rs index 57c8f9e..e7426ed 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,6 +1,6 @@ /* * tmtd - Suckless To Do list - * Copyright (C) 2022 lemon.sh & famfo + * Copyright (C) 2022 lemon.sh & karx * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free diff --git a/src/main.rs b/src/main.rs index a52734d..08837f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ /* * tmtd - Suckless To Do list - * Copyright (C) 2022 lemon.sh & famfo + * Copyright (C) 2022 lemon.sh & karx * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free diff --git a/src/web.rs b/src/web.rs index 91764b8..ae562fd 100644 --- a/src/web.rs +++ b/src/web.rs @@ -1,6 +1,6 @@ /* * tmtd - Suckless To Do list - * Copyright (C) 2022 lemon.sh & famfo + * Copyright (C) 2022 lemon.sh & karx * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free @@ -19,13 +19,14 @@ use crate::{Config, Database}; use std::sync::Arc; use tokio::sync::broadcast; -use warp::Filter; -use tera::Tera; +use warp::{Filter, reply}; +use tera::{Context, Tera}; +use tracing::info; pub struct App { config: Arc, db: Arc, - tera: Tera + tera: Arc } impl App { @@ -33,17 +34,25 @@ impl App { Ok(Self { config, db, - tera: Tera::new("templates/**/*.html")? + tera: Arc::new(Tera::new("templates/*.html")?) }) } pub async fn run(self, mut cancel: broadcast::Receiver<()>) { - let hello = warp::path!("hello" / String).map(|name| format!("Hello, {}!", name)); + let hello = warp::path!("hello" / String).map({ + let tera = self.tera.clone(); + move |name: String| { + let mut ctx = Context::new(); + ctx.insert("name", &name); + reply::html(tera.render("hello.html", &ctx).unwrap()) + } + }); let (_, server) = warp::serve(hello).bind_with_graceful_shutdown(self.config.listen_addr, async move { cancel.recv().await.unwrap(); }); server.await; + info!("Web app has been shut down") } -} \ No newline at end of file +} diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..3b559a3 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,10 @@ + + + + + tmtd + + +{% block content %} {% endblock %} + + \ No newline at end of file diff --git a/templates/hello.html b/templates/hello.html new file mode 100644 index 0000000..4618656 --- /dev/null +++ b/templates/hello.html @@ -0,0 +1,5 @@ +{% extends "base.html" %} + +{% block content %} +Hello, {{name}} +{% endblock content %} \ No newline at end of file