impl render to template class
This commit is contained in:
parent
6d3b4c8398
commit
eb9e7e22f3
8 changed files with 36 additions and 38 deletions
|
@ -8,6 +8,8 @@ use glob::glob;
|
||||||
use minify_html::{minify, Cfg};
|
use minify_html::{minify, Cfg};
|
||||||
use std::{fs::File, io::Write, path::Path};
|
use std::{fs::File, io::Write, path::Path};
|
||||||
|
|
||||||
|
use crate::template::Template;
|
||||||
|
|
||||||
#[derive(Deserialize, Clone, Default)]
|
#[derive(Deserialize, Clone, Default)]
|
||||||
pub struct FileConfig {
|
pub struct FileConfig {
|
||||||
pub scheme: Option<String>,
|
pub scheme: Option<String>,
|
||||||
|
@ -17,13 +19,6 @@ pub struct FileConfig {
|
||||||
pub onion: Option<String>,
|
pub onion: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Config {
|
|
||||||
pub fc: FileConfig,
|
|
||||||
pub static_location: String,
|
|
||||||
pub templates_location: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FileConfig {
|
impl FileConfig {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
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 {
|
fn get_file_config(file_path: &str) -> FileConfig {
|
||||||
match fs::read_to_string(file_path) {
|
match fs::read_to_string(file_path) {
|
||||||
Ok(file) => match toml::from_str(&file) {
|
Ok(file) => match toml::from_str(&file) {
|
||||||
|
@ -86,7 +88,9 @@ pub fn get_config(file_path: &str) -> Config {
|
||||||
Config {
|
Config {
|
||||||
fc: internal_config,
|
fc: internal_config,
|
||||||
static_location: format!("{}/{}", files_root, static_dir),
|
static_location: format!("{}/{}", files_root, static_dir),
|
||||||
templates_location: format!("{}/{}", files_root, templates_dir),
|
tmpl: Template {
|
||||||
|
directory: format!("{}/{}", files_root, templates_dir),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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 actix_web::{dev::ConnectionInfo, get, routes, web, HttpRequest, HttpResponse, Responder};
|
||||||
use ramhorns::Content;
|
use ramhorns::Content;
|
||||||
|
|
||||||
|
@ -20,8 +20,7 @@ struct SecurityTemplate {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_security(config: Config, info: ConnectionInfo) -> String {
|
fn get_security(config: Config, info: ConnectionInfo) -> String {
|
||||||
render(
|
config.tmpl.render(
|
||||||
config.templates_location,
|
|
||||||
"security.txt",
|
"security.txt",
|
||||||
SecurityTemplate {
|
SecurityTemplate {
|
||||||
contact: config.fc.mail.unwrap_or_default(),
|
contact: config.fc.mail.unwrap_or_default(),
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{config::Config, template::render};
|
use crate::config::Config;
|
||||||
use actix_web::{get, web, HttpResponse, Responder};
|
use actix_web::{get, web, HttpResponse, Responder};
|
||||||
use ramhorns::Content;
|
use ramhorns::Content;
|
||||||
|
|
||||||
|
@ -11,9 +11,5 @@ pub async fn page(config: web::Data<Config>) -> impl Responder {
|
||||||
struct PortfolioTemplate {}
|
struct PortfolioTemplate {}
|
||||||
|
|
||||||
pub fn get_page(config: Config) -> std::string::String {
|
pub fn get_page(config: Config) -> std::string::String {
|
||||||
render(
|
config.tmpl.render("contrib.html", PortfolioTemplate {})
|
||||||
config.templates_location,
|
|
||||||
"contrib.html",
|
|
||||||
PortfolioTemplate {},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use actix_web::{get, web, HttpResponse, Responder};
|
use actix_web::{get, web, HttpResponse, Responder};
|
||||||
use ramhorns::Content;
|
use ramhorns::Content;
|
||||||
|
|
||||||
use crate::{config::Config, template::render};
|
use crate::config::Config;
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
pub async fn page(config: web::Data<Config>) -> impl Responder {
|
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 {}
|
struct IndexTemplate {}
|
||||||
|
|
||||||
pub fn get_page(config: Config) -> std::string::String {
|
pub fn get_page(config: Config) -> std::string::String {
|
||||||
render(config.templates_location, "index.html", IndexTemplate {})
|
config.tmpl.render("index.html", IndexTemplate {})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use actix_web::{get, web, HttpResponse, Responder};
|
use actix_web::{get, web, HttpResponse, Responder};
|
||||||
use ramhorns::Content;
|
use ramhorns::Content;
|
||||||
|
|
||||||
use crate::{config::Config, template::render};
|
use crate::config::Config;
|
||||||
|
|
||||||
#[get("/networks")]
|
#[get("/networks")]
|
||||||
pub async fn page(config: web::Data<Config>) -> impl Responder {
|
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 {}
|
struct NetworksTemplate {}
|
||||||
|
|
||||||
pub fn get_page(config: Config) -> std::string::String {
|
pub fn get_page(config: Config) -> std::string::String {
|
||||||
render(
|
config.tmpl.render("networks.html", NetworksTemplate {})
|
||||||
config.templates_location,
|
|
||||||
"networks.html",
|
|
||||||
NetworksTemplate {},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use actix_web::{web, HttpResponse, Responder};
|
use actix_web::{web, HttpResponse, Responder};
|
||||||
use ramhorns::Content;
|
use ramhorns::Content;
|
||||||
|
|
||||||
use crate::{config::Config, template::render};
|
use crate::config::Config;
|
||||||
|
|
||||||
pub async fn page(config: web::Data<Config>) -> impl Responder {
|
pub async fn page(config: web::Data<Config>) -> impl Responder {
|
||||||
HttpResponse::NotFound().body(get_page(config.get_ref().clone()))
|
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 {}
|
struct Error404Template {}
|
||||||
|
|
||||||
pub fn get_page(config: Config) -> std::string::String {
|
pub fn get_page(config: Config) -> std::string::String {
|
||||||
render(config.templates_location, "404.html", Error404Template {})
|
config.tmpl.render("404.html", Error404Template {})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use actix_web::{get, web, HttpResponse, Responder};
|
use actix_web::{get, web, HttpResponse, Responder};
|
||||||
use ramhorns::Content;
|
use ramhorns::Content;
|
||||||
|
|
||||||
use crate::{config::Config, template::render};
|
use crate::config::Config;
|
||||||
|
|
||||||
#[get("/portfolio")]
|
#[get("/portfolio")]
|
||||||
pub async fn page(config: web::Data<Config>) -> impl Responder {
|
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 {}
|
struct PortfolioTemplate {}
|
||||||
|
|
||||||
pub fn get_page(config: Config) -> std::string::String {
|
pub fn get_page(config: Config) -> std::string::String {
|
||||||
render(
|
config.tmpl.render("portfolio.html", PortfolioTemplate {})
|
||||||
config.templates_location,
|
|
||||||
"portfolio.html",
|
|
||||||
PortfolioTemplate {},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,15 @@
|
||||||
use ramhorns::{Content, Ramhorns};
|
use ramhorns::{Content, Ramhorns};
|
||||||
|
|
||||||
pub fn render<C: Content>(template_dir: String, template: &str, data: C) -> String {
|
#[derive(Clone)]
|
||||||
let mut tpls: Ramhorns = Ramhorns::lazy(template_dir).unwrap();
|
pub struct Template {
|
||||||
let tpl = tpls.from_file(template).unwrap();
|
pub directory: String,
|
||||||
|
}
|
||||||
tpl.render(&data)
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue