impl render to template class
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending
ci/woodpecker/pr/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-09 19:26:20 +02:00
parent 6d3b4c8398
commit eb9e7e22f3
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
8 changed files with 36 additions and 38 deletions

View file

@ -8,6 +8,8 @@ use glob::glob;
use minify_html::{minify, Cfg};
use std::{fs::File, io::Write, path::Path};
use crate::template::Template;
#[derive(Deserialize, Clone, Default)]
pub struct FileConfig {
pub scheme: Option<String>,
@ -17,13 +19,6 @@ pub struct FileConfig {
pub onion: Option<String>,
}
#[derive(Clone)]
pub struct Config {
pub fc: FileConfig,
pub static_location: String,
pub templates_location: String,
}
impl FileConfig {
fn new() -> Self {
Self {
@ -56,6 +51,13 @@ impl FileConfig {
}
}
#[derive(Clone)]
pub struct Config {
pub fc: FileConfig,
pub static_location: String,
pub tmpl: Template,
}
fn get_file_config(file_path: &str) -> FileConfig {
match fs::read_to_string(file_path) {
Ok(file) => match toml::from_str(&file) {
@ -86,7 +88,9 @@ pub fn get_config(file_path: &str) -> Config {
Config {
fc: internal_config,
static_location: format!("{}/{}", files_root, static_dir),
templates_location: format!("{}/{}", files_root, templates_dir),
tmpl: Template {
directory: format!("{}/{}", files_root, templates_dir),
},
}
}

View file

@ -1,4 +1,4 @@
use crate::{config::Config, template::render};
use crate::config::Config;
use actix_web::{dev::ConnectionInfo, get, routes, web, HttpRequest, HttpResponse, Responder};
use ramhorns::Content;
@ -20,8 +20,7 @@ struct SecurityTemplate {
}
fn get_security(config: Config, info: ConnectionInfo) -> String {
render(
config.templates_location,
config.tmpl.render(
"security.txt",
SecurityTemplate {
contact: config.fc.mail.unwrap_or_default(),

View file

@ -1,4 +1,4 @@
use crate::{config::Config, template::render};
use crate::config::Config;
use actix_web::{get, web, HttpResponse, Responder};
use ramhorns::Content;
@ -11,9 +11,5 @@ pub async fn page(config: web::Data<Config>) -> impl Responder {
struct PortfolioTemplate {}
pub fn get_page(config: Config) -> std::string::String {
render(
config.templates_location,
"contrib.html",
PortfolioTemplate {},
)
config.tmpl.render("contrib.html", PortfolioTemplate {})
}

View file

@ -1,7 +1,7 @@
use actix_web::{get, web, HttpResponse, Responder};
use ramhorns::Content;
use crate::{config::Config, template::render};
use crate::config::Config;
#[get("/")]
pub async fn page(config: web::Data<Config>) -> impl Responder {
@ -12,5 +12,5 @@ pub async fn page(config: web::Data<Config>) -> impl Responder {
struct IndexTemplate {}
pub fn get_page(config: Config) -> std::string::String {
render(config.templates_location, "index.html", IndexTemplate {})
config.tmpl.render("index.html", IndexTemplate {})
}

View file

@ -1,7 +1,7 @@
use actix_web::{get, web, HttpResponse, Responder};
use ramhorns::Content;
use crate::{config::Config, template::render};
use crate::config::Config;
#[get("/networks")]
pub async fn page(config: web::Data<Config>) -> impl Responder {
@ -12,9 +12,5 @@ pub async fn page(config: web::Data<Config>) -> impl Responder {
struct NetworksTemplate {}
pub fn get_page(config: Config) -> std::string::String {
render(
config.templates_location,
"networks.html",
NetworksTemplate {},
)
config.tmpl.render("networks.html", NetworksTemplate {})
}

View file

@ -1,7 +1,7 @@
use actix_web::{web, HttpResponse, Responder};
use ramhorns::Content;
use crate::{config::Config, template::render};
use crate::config::Config;
pub async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::NotFound().body(get_page(config.get_ref().clone()))
@ -11,5 +11,5 @@ pub async fn page(config: web::Data<Config>) -> impl Responder {
struct Error404Template {}
pub fn get_page(config: Config) -> std::string::String {
render(config.templates_location, "404.html", Error404Template {})
config.tmpl.render("404.html", Error404Template {})
}

View file

@ -1,7 +1,7 @@
use actix_web::{get, web, HttpResponse, Responder};
use ramhorns::Content;
use crate::{config::Config, template::render};
use crate::config::Config;
#[get("/portfolio")]
pub async fn page(config: web::Data<Config>) -> impl Responder {
@ -12,9 +12,5 @@ pub async fn page(config: web::Data<Config>) -> impl Responder {
struct PortfolioTemplate {}
pub fn get_page(config: Config) -> std::string::String {
render(
config.templates_location,
"portfolio.html",
PortfolioTemplate {},
)
config.tmpl.render("portfolio.html", PortfolioTemplate {})
}

View file

@ -1,8 +1,15 @@
use ramhorns::{Content, Ramhorns};
pub fn render<C: Content>(template_dir: String, template: &str, data: C) -> String {
let mut tpls: Ramhorns = Ramhorns::lazy(template_dir).unwrap();
let tpl = tpls.from_file(template).unwrap();
tpl.render(&data)
#[derive(Clone)]
pub struct Template {
pub directory: String,
}
impl Template {
pub fn render<C: Content>(&self, template: &str, data: C) -> String {
let mut templates: Ramhorns = Ramhorns::lazy(&self.directory).unwrap();
let tplt = templates.from_file(template).unwrap();
tplt.render(&data)
}
}