Templating works somehow

This commit is contained in:
lemonsh 2022-04-08 18:54:08 +02:00
parent 4ce64b536c
commit 98dc77d7ba
6 changed files with 34 additions and 10 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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<Config>,
db: Arc<Database>,
tera: Tera
tera: Arc<Tera>
}
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")
}
}
}

10
templates/base.html Normal file
View file

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tmtd</title>
</head>
<body>
{% block content %} {% endblock %}
</body>
</html>

5
templates/hello.html Normal file
View file

@ -0,0 +1,5 @@
{% extends "base.html" %}
{% block content %}
Hello, {{name}}
{% endblock content %}