diff --git a/src/misc/mod.rs b/src/misc/mod.rs index 725c25c..317a46a 100644 --- a/src/misc/mod.rs +++ b/src/misc/mod.rs @@ -1,4 +1,4 @@ pub mod date; pub mod github; pub mod markdown; -mod utils; +pub mod utils; diff --git a/src/misc/utils.rs b/src/misc/utils.rs index b740952..9c32c91 100644 --- a/src/misc/utils.rs +++ b/src/misc/utils.rs @@ -1,3 +1,6 @@ +use std::cell::Ref; + +use actix_web::dev::ConnectionInfo; use cached::proc_macro::cached; use reqwest::Client; @@ -8,3 +11,8 @@ pub fn get_reqwest_client() -> Client { .build() .unwrap() } + +/// Get URL of the app +pub fn get_url(info: Ref<'_, ConnectionInfo>) -> String { + format!("{}://{}", info.scheme(), info.host()) +} diff --git a/src/routes/agreements.rs b/src/routes/agreements.rs index 575f725..e6bcb60 100644 --- a/src/routes/agreements.rs +++ b/src/routes/agreements.rs @@ -1,5 +1,5 @@ -use crate::{config::Config, template::Infos}; -use actix_web::{dev::ConnectionInfo, get, routes, web, HttpRequest, HttpResponse, Responder}; +use crate::{config::Config, misc::utils::get_url, template::Infos}; +use actix_web::{get, routes, web, HttpRequest, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; @@ -9,7 +9,7 @@ use ramhorns::Content; pub async fn security(req: HttpRequest, config: web::Data) -> impl Responder { HttpResponse::Ok().body(build_securitytxt( config.get_ref().to_owned(), - req.connection_info().to_owned(), + get_url(req.connection_info()), )) } @@ -21,17 +21,13 @@ struct SecurityTemplate { } #[once(time = 60)] -fn build_securitytxt(config: Config, info: ConnectionInfo) -> String { +fn build_securitytxt(config: Config, url: String) -> String { config.tmpl.render( "security.txt", SecurityTemplate { contact: config.fc.mail.unwrap_or_default(), pref_lang: config.fc.lang.unwrap_or_default(), - url: format!( - "{}://{}/.well-known/security.txt", - info.scheme(), - info.host() - ), + url: format!("{}/.well-known/security.txt", url), }, Infos::default(), ) diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 076393f..745248b 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -19,6 +19,7 @@ use crate::{ misc::{ date::Date, markdown::{get_metadata, get_options, read_file, File, FileMetadata}, + utils::get_url, }, template::Infos, }; @@ -26,8 +27,11 @@ use crate::{ const MIME_TYPE_RSS: &str = "application/rss+xml"; #[get("/blog")] -pub async fn index(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_index(config.get_ref().to_owned())) +pub async fn index(req: HttpRequest, config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_index( + config.get_ref().to_owned(), + get_url(req.connection_info()), + )) } #[derive(Content)] @@ -37,7 +41,7 @@ struct BlogIndexTemplate { } #[once(time = 120)] -pub fn build_index(config: Config) -> String { +pub fn build_index(config: Config, url: String) -> String { let mut posts = get_posts("data/blog"); // Sort from newest to oldest @@ -57,6 +61,7 @@ pub fn build_index(config: Config) -> String { config.fc.name.unwrap_or_default() )), page_kw: Some(["blog", "blogging"].join(", ")), + url, }, ) } @@ -156,20 +161,28 @@ struct BlogPostTemplate { } #[get("/blog/p/{id}")] -pub async fn page(path: web::Path<(String,)>, config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_post(path.into_inner().0, config.get_ref().to_owned())) +pub async fn page( + req: HttpRequest, + path: web::Path<(String,)>, + config: web::Data, +) -> impl Responder { + HttpResponse::Ok().body(build_post( + path.into_inner().0, + config.get_ref().to_owned(), + get_url(req.connection_info()), + )) } -fn build_post(file: String, config: Config) -> String { +fn build_post(file: String, config: Config, url: String) -> String { let mut post = None; - let infos = get_post(&mut post, file, config.fc.name.unwrap_or_default()); + let infos = get_post(&mut post, file, config.fc.name.unwrap_or_default(), url); config .tmpl .render("blog/post.html", BlogPostTemplate { post }, infos) } -fn get_post(post: &mut Option, filename: String, name: String) -> Infos { +fn get_post(post: &mut Option, filename: String, name: String, url: String) -> Infos { let blog_dir = "data/blog"; let ext = ".md"; @@ -187,6 +200,7 @@ fn get_post(post: &mut Option, filename: String, name: String) -> Infos { page_title: Some(format!("Post: {}", title)), page_desc: Some(format!("Blog d'{name}")), page_kw: Some(["blog", "blogging", "write", "writing"].join(", ")), + url, } } diff --git a/src/routes/contrib.rs b/src/routes/contrib.rs index 4f091e6..ae54e89 100644 --- a/src/routes/contrib.rs +++ b/src/routes/contrib.rs @@ -2,16 +2,20 @@ use std::collections::HashMap; use crate::{ config::Config, - misc::github::{fetch_pr, ProjectState}, + misc::{ + github::{fetch_pr, ProjectState}, + utils::get_url, + }, template::Infos, }; -use actix_web::{get, web, HttpResponse, Responder}; +use actix_web::{get, web, HttpRequest, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; #[get("/contrib")] -pub async fn page(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_page(config.get_ref().to_owned()).await) +pub async fn page(req: HttpRequest, config: web::Data) -> impl Responder { + let url = get_url(req.connection_info()); + HttpResponse::Ok().body(build_page(config.get_ref().to_owned(), url).await) } #[derive(Content)] @@ -41,7 +45,7 @@ struct Pull { } #[once(time = 120)] -pub async fn build_page(config: Config) -> String { +pub async fn build_page(config: Config, url: String) -> String { // Fetch latest data from github let data = match fetch_pr().await { Ok(projects) => { @@ -146,6 +150,7 @@ pub async fn build_page(config: Config) -> String { config.fc.name.unwrap_or_default() )), page_kw: None, + url, }, ) } diff --git a/src/routes/index.rs b/src/routes/index.rs index 6e133ca..e09e85b 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -1,15 +1,18 @@ -use actix_web::{get, web, HttpResponse, Responder}; +use actix_web::{get, web, HttpRequest, HttpResponse, Responder}; use cached::proc_macro::once; -use crate::{config::Config, template::Infos}; +use crate::{config::Config, misc::utils::get_url, template::Infos}; #[get("/")] -pub async fn page(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) +pub async fn page(req: HttpRequest, config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_page( + config.get_ref().to_owned(), + get_url(req.connection_info()), + )) } #[once(time = 60)] -pub fn build_page(config: Config) -> String { +pub fn build_page(config: Config, url: String) -> String { config.tmpl.render( "index.html", (), @@ -17,6 +20,7 @@ pub fn build_page(config: Config) -> String { page_title: config.fc.fullname, page_desc: Some("Page principale".into()), page_kw: None, + url, }, ) } diff --git a/src/routes/networks.rs b/src/routes/networks.rs index d2161b6..7608a2b 100644 --- a/src/routes/networks.rs +++ b/src/routes/networks.rs @@ -1,15 +1,18 @@ -use actix_web::{get, web, HttpResponse, Responder}; +use actix_web::{get, web, HttpRequest, HttpResponse, Responder}; use cached::proc_macro::once; -use crate::{config::Config, template::Infos}; +use crate::{config::Config, misc::utils::get_url, template::Infos}; #[get("/networks")] -pub async fn page(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) +pub async fn page(req: HttpRequest, config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_page( + config.get_ref().to_owned(), + get_url(req.connection_info()), + )) } #[once(time = 60)] -pub fn build_page(config: Config) -> String { +pub fn build_page(config: Config, url: String) -> String { config.tmpl.render( "networks.html", (), @@ -17,6 +20,7 @@ pub fn build_page(config: Config) -> String { page_title: Some("Mes réseaux".into()), page_desc: Some(format!("Réseaux d'{}", config.fc.name.unwrap_or_default())), page_kw: None, + url, }, ) } diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index e78d425..a79b61f 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -1,17 +1,23 @@ -use actix_web::{get, web, HttpResponse, Responder}; +use actix_web::{get, web, HttpRequest, HttpResponse, Responder}; use cached::proc_macro::once; use glob::glob; use ramhorns::Content; use crate::{ config::Config, - misc::markdown::{read_file, File}, + misc::{ + markdown::{read_file, File}, + utils::get_url, + }, template::Infos, }; #[get("/portfolio")] -pub async fn page(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) +pub async fn page(req: HttpRequest, config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_page( + config.get_ref().to_owned(), + get_url(req.connection_info()), + )) } #[derive(Content)] @@ -26,7 +32,7 @@ struct PortfolioTemplate<'a> { } #[once(time = 60)] -pub fn build_page(config: Config) -> String { +pub fn build_page(config: Config, url: String) -> String { let projects_dir = "data/projects"; let ext = ".md"; @@ -81,6 +87,7 @@ pub fn build_page(config: Config) -> String { config.fc.name.unwrap_or_default() )), page_kw: None, + url, }, ) } diff --git a/src/routes/web3.rs b/src/routes/web3.rs index 7b632f9..93e2c9e 100644 --- a/src/routes/web3.rs +++ b/src/routes/web3.rs @@ -1,15 +1,18 @@ -use actix_web::{get, web, HttpResponse, Responder}; +use actix_web::{get, web, HttpRequest, HttpResponse, Responder}; use cached::proc_macro::once; -use crate::{config::Config, template::Infos}; +use crate::{config::Config, misc::utils::get_url, template::Infos}; #[get("/web3")] -pub async fn page(config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) +pub async fn page(req: HttpRequest, config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_page( + config.get_ref().to_owned(), + get_url(req.connection_info()), + )) } #[once(time = 60)] -pub fn build_page(config: Config) -> String { +pub fn build_page(config: Config, url: String) -> String { config.tmpl.render( "web3.html", (), @@ -17,6 +20,7 @@ pub fn build_page(config: Config) -> String { page_title: Some("Mylloon".into()), page_desc: Some("Coin reculé de l'internet".into()), page_kw: None, + url, }, ) } diff --git a/src/template.rs b/src/template.rs index a24c2db..d93ddb7 100644 --- a/src/template.rs +++ b/src/template.rs @@ -18,6 +18,8 @@ pub struct Infos { pub page_desc: Option, /// Keywords pub page_kw: Option, + /// URL of the website + pub url: String, } /// Final structure given to template