diff --git a/src/config.rs b/src/config.rs index d0bf191..acf6af0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, @@ -17,13 +19,6 @@ pub struct FileConfig { pub onion: Option, } -#[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), + }, } } diff --git a/src/routes/agreements.rs b/src/routes/agreements.rs index 4d61bdb..9a5c570 100644 --- a/src/routes/agreements.rs +++ b/src/routes/agreements.rs @@ -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(), diff --git a/src/routes/contrib.rs b/src/routes/contrib.rs index 265684f..50ed003 100644 --- a/src/routes/contrib.rs +++ b/src/routes/contrib.rs @@ -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) -> 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 {}) } diff --git a/src/routes/index.rs b/src/routes/index.rs index b687834..f985d30 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -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) -> impl Responder { @@ -12,5 +12,5 @@ pub async fn page(config: web::Data) -> 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 {}) } diff --git a/src/routes/networks.rs b/src/routes/networks.rs index 58a6c44..d726919 100644 --- a/src/routes/networks.rs +++ b/src/routes/networks.rs @@ -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) -> impl Responder { @@ -12,9 +12,5 @@ pub async fn page(config: web::Data) -> 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 {}) } diff --git a/src/routes/not_found.rs b/src/routes/not_found.rs index 2da5c3a..9e57b20 100644 --- a/src/routes/not_found.rs +++ b/src/routes/not_found.rs @@ -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) -> impl Responder { HttpResponse::NotFound().body(get_page(config.get_ref().clone())) @@ -11,5 +11,5 @@ pub async fn page(config: web::Data) -> 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 {}) } diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index 328838d..98386e0 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -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) -> impl Responder { @@ -12,9 +12,5 @@ pub async fn page(config: web::Data) -> 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 {}) } diff --git a/src/template.rs b/src/template.rs index 82fee19..1c8e015 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,8 +1,15 @@ use ramhorns::{Content, Ramhorns}; -pub fn render(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(&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) + } }