Initial commit
This commit is contained in:
commit
980a85d41b
15 changed files with 3969 additions and 0 deletions
45
src/templates.rs
Normal file
45
src/templates.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
use crate::db::*;
|
||||
|
||||
use serde::Serialize;
|
||||
use std::path::Path;
|
||||
use tera::Tera;
|
||||
|
||||
static TEMPLATE_FILES: &[(&str, &str)] = &[
|
||||
("comments.html", include_str!("../templates/comments.html")),
|
||||
(
|
||||
"admin_login.html",
|
||||
include_str!("../templates/admin_login.html"),
|
||||
),
|
||||
];
|
||||
|
||||
pub struct Templates {
|
||||
pub tera: Tera,
|
||||
}
|
||||
|
||||
impl Templates {
|
||||
pub fn new(dir: &Path) -> Self {
|
||||
let dir = dir.join("templates");
|
||||
std::fs::create_dir_all(&dir).expect("Cannot create templates dir");
|
||||
|
||||
for &(file, default) in TEMPLATE_FILES {
|
||||
let file_path = dir.join(file);
|
||||
if !file_path.is_file() {
|
||||
std::fs::write(file_path, default)
|
||||
.unwrap_or_else(|_| panic!("Cannot write template file {}", file));
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
tera: Tera::new(dir.join("**/*").to_str().unwrap()).expect("Failed to parse templates"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize)]
|
||||
pub struct CommentWithId {
|
||||
pub author: String,
|
||||
pub id: String,
|
||||
pub needs_approval: bool,
|
||||
pub post_time: Time,
|
||||
pub text: String,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue