give current url to all files for metadata
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-29 14:09:41 +02:00
parent 6c78778373
commit 16b9427612
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
10 changed files with 87 additions and 43 deletions

View file

@ -1,4 +1,4 @@
pub mod date; pub mod date;
pub mod github; pub mod github;
pub mod markdown; pub mod markdown;
mod utils; pub mod utils;

View file

@ -1,3 +1,6 @@
use std::cell::Ref;
use actix_web::dev::ConnectionInfo;
use cached::proc_macro::cached; use cached::proc_macro::cached;
use reqwest::Client; use reqwest::Client;
@ -8,3 +11,8 @@ pub fn get_reqwest_client() -> Client {
.build() .build()
.unwrap() .unwrap()
} }
/// Get URL of the app
pub fn get_url(info: Ref<'_, ConnectionInfo>) -> String {
format!("{}://{}", info.scheme(), info.host())
}

View file

@ -1,5 +1,5 @@
use crate::{config::Config, template::Infos}; use crate::{config::Config, misc::utils::get_url, template::Infos};
use actix_web::{dev::ConnectionInfo, get, routes, web, HttpRequest, HttpResponse, Responder}; use actix_web::{get, routes, web, HttpRequest, HttpResponse, Responder};
use cached::proc_macro::once; use cached::proc_macro::once;
use ramhorns::Content; use ramhorns::Content;
@ -9,7 +9,7 @@ use ramhorns::Content;
pub async fn security(req: HttpRequest, config: web::Data<Config>) -> impl Responder { pub async fn security(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_securitytxt( HttpResponse::Ok().body(build_securitytxt(
config.get_ref().to_owned(), config.get_ref().to_owned(),
req.connection_info().to_owned(), get_url(req.connection_info()),
)) ))
} }
@ -21,17 +21,13 @@ struct SecurityTemplate {
} }
#[once(time = 60)] #[once(time = 60)]
fn build_securitytxt(config: Config, info: ConnectionInfo) -> String { fn build_securitytxt(config: Config, url: String) -> String {
config.tmpl.render( config.tmpl.render(
"security.txt", "security.txt",
SecurityTemplate { SecurityTemplate {
contact: config.fc.mail.unwrap_or_default(), contact: config.fc.mail.unwrap_or_default(),
pref_lang: config.fc.lang.unwrap_or_default(), pref_lang: config.fc.lang.unwrap_or_default(),
url: format!( url: format!("{}/.well-known/security.txt", url),
"{}://{}/.well-known/security.txt",
info.scheme(),
info.host()
),
}, },
Infos::default(), Infos::default(),
) )

View file

@ -19,6 +19,7 @@ use crate::{
misc::{ misc::{
date::Date, date::Date,
markdown::{get_metadata, get_options, read_file, File, FileMetadata}, markdown::{get_metadata, get_options, read_file, File, FileMetadata},
utils::get_url,
}, },
template::Infos, template::Infos,
}; };
@ -26,8 +27,11 @@ use crate::{
const MIME_TYPE_RSS: &str = "application/rss+xml"; const MIME_TYPE_RSS: &str = "application/rss+xml";
#[get("/blog")] #[get("/blog")]
pub async fn index(config: web::Data<Config>) -> impl Responder { pub async fn index(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_index(config.get_ref().to_owned())) HttpResponse::Ok().body(build_index(
config.get_ref().to_owned(),
get_url(req.connection_info()),
))
} }
#[derive(Content)] #[derive(Content)]
@ -37,7 +41,7 @@ struct BlogIndexTemplate {
} }
#[once(time = 120)] #[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"); let mut posts = get_posts("data/blog");
// Sort from newest to oldest // Sort from newest to oldest
@ -57,6 +61,7 @@ pub fn build_index(config: Config) -> String {
config.fc.name.unwrap_or_default() config.fc.name.unwrap_or_default()
)), )),
page_kw: Some(["blog", "blogging"].join(", ")), page_kw: Some(["blog", "blogging"].join(", ")),
url,
}, },
) )
} }
@ -156,20 +161,28 @@ struct BlogPostTemplate {
} }
#[get("/blog/p/{id}")] #[get("/blog/p/{id}")]
pub async fn page(path: web::Path<(String,)>, config: web::Data<Config>) -> impl Responder { pub async fn page(
HttpResponse::Ok().body(build_post(path.into_inner().0, config.get_ref().to_owned())) req: HttpRequest,
path: web::Path<(String,)>,
config: web::Data<Config>,
) -> 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 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 config
.tmpl .tmpl
.render("blog/post.html", BlogPostTemplate { post }, infos) .render("blog/post.html", BlogPostTemplate { post }, infos)
} }
fn get_post(post: &mut Option<File>, filename: String, name: String) -> Infos { fn get_post(post: &mut Option<File>, filename: String, name: String, url: String) -> Infos {
let blog_dir = "data/blog"; let blog_dir = "data/blog";
let ext = ".md"; let ext = ".md";
@ -187,6 +200,7 @@ fn get_post(post: &mut Option<File>, filename: String, name: String) -> Infos {
page_title: Some(format!("Post: {}", title)), page_title: Some(format!("Post: {}", title)),
page_desc: Some(format!("Blog d'{name}")), page_desc: Some(format!("Blog d'{name}")),
page_kw: Some(["blog", "blogging", "write", "writing"].join(", ")), page_kw: Some(["blog", "blogging", "write", "writing"].join(", ")),
url,
} }
} }

View file

@ -2,16 +2,20 @@ use std::collections::HashMap;
use crate::{ use crate::{
config::Config, config::Config,
misc::github::{fetch_pr, ProjectState}, misc::{
github::{fetch_pr, ProjectState},
utils::get_url,
},
template::Infos, template::Infos,
}; };
use actix_web::{get, web, HttpResponse, Responder}; use actix_web::{get, web, HttpRequest, HttpResponse, Responder};
use cached::proc_macro::once; use cached::proc_macro::once;
use ramhorns::Content; use ramhorns::Content;
#[get("/contrib")] #[get("/contrib")]
pub async fn page(config: web::Data<Config>) -> impl Responder { pub async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page(config.get_ref().to_owned()).await) let url = get_url(req.connection_info());
HttpResponse::Ok().body(build_page(config.get_ref().to_owned(), url).await)
} }
#[derive(Content)] #[derive(Content)]
@ -41,7 +45,7 @@ struct Pull {
} }
#[once(time = 120)] #[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 // Fetch latest data from github
let data = match fetch_pr().await { let data = match fetch_pr().await {
Ok(projects) => { Ok(projects) => {
@ -146,6 +150,7 @@ pub async fn build_page(config: Config) -> String {
config.fc.name.unwrap_or_default() config.fc.name.unwrap_or_default()
)), )),
page_kw: None, page_kw: None,
url,
}, },
) )
} }

View file

@ -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 cached::proc_macro::once;
use crate::{config::Config, template::Infos}; use crate::{config::Config, misc::utils::get_url, template::Infos};
#[get("/")] #[get("/")]
pub async fn page(config: web::Data<Config>) -> impl Responder { pub async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) HttpResponse::Ok().body(build_page(
config.get_ref().to_owned(),
get_url(req.connection_info()),
))
} }
#[once(time = 60)] #[once(time = 60)]
pub fn build_page(config: Config) -> String { pub fn build_page(config: Config, url: String) -> String {
config.tmpl.render( config.tmpl.render(
"index.html", "index.html",
(), (),
@ -17,6 +20,7 @@ pub fn build_page(config: Config) -> String {
page_title: config.fc.fullname, page_title: config.fc.fullname,
page_desc: Some("Page principale".into()), page_desc: Some("Page principale".into()),
page_kw: None, page_kw: None,
url,
}, },
) )
} }

View file

@ -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 cached::proc_macro::once;
use crate::{config::Config, template::Infos}; use crate::{config::Config, misc::utils::get_url, template::Infos};
#[get("/networks")] #[get("/networks")]
pub async fn page(config: web::Data<Config>) -> impl Responder { pub async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) HttpResponse::Ok().body(build_page(
config.get_ref().to_owned(),
get_url(req.connection_info()),
))
} }
#[once(time = 60)] #[once(time = 60)]
pub fn build_page(config: Config) -> String { pub fn build_page(config: Config, url: String) -> String {
config.tmpl.render( config.tmpl.render(
"networks.html", "networks.html",
(), (),
@ -17,6 +20,7 @@ pub fn build_page(config: Config) -> String {
page_title: Some("Mes réseaux".into()), page_title: Some("Mes réseaux".into()),
page_desc: Some(format!("Réseaux d'{}", config.fc.name.unwrap_or_default())), page_desc: Some(format!("Réseaux d'{}", config.fc.name.unwrap_or_default())),
page_kw: None, page_kw: None,
url,
}, },
) )
} }

View file

@ -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 cached::proc_macro::once;
use glob::glob; use glob::glob;
use ramhorns::Content; use ramhorns::Content;
use crate::{ use crate::{
config::Config, config::Config,
misc::markdown::{read_file, File}, misc::{
markdown::{read_file, File},
utils::get_url,
},
template::Infos, template::Infos,
}; };
#[get("/portfolio")] #[get("/portfolio")]
pub async fn page(config: web::Data<Config>) -> impl Responder { pub async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) HttpResponse::Ok().body(build_page(
config.get_ref().to_owned(),
get_url(req.connection_info()),
))
} }
#[derive(Content)] #[derive(Content)]
@ -26,7 +32,7 @@ struct PortfolioTemplate<'a> {
} }
#[once(time = 60)] #[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 projects_dir = "data/projects";
let ext = ".md"; let ext = ".md";
@ -81,6 +87,7 @@ pub fn build_page(config: Config) -> String {
config.fc.name.unwrap_or_default() config.fc.name.unwrap_or_default()
)), )),
page_kw: None, page_kw: None,
url,
}, },
) )
} }

View file

@ -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 cached::proc_macro::once;
use crate::{config::Config, template::Infos}; use crate::{config::Config, misc::utils::get_url, template::Infos};
#[get("/web3")] #[get("/web3")]
pub async fn page(config: web::Data<Config>) -> impl Responder { pub async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) HttpResponse::Ok().body(build_page(
config.get_ref().to_owned(),
get_url(req.connection_info()),
))
} }
#[once(time = 60)] #[once(time = 60)]
pub fn build_page(config: Config) -> String { pub fn build_page(config: Config, url: String) -> String {
config.tmpl.render( config.tmpl.render(
"web3.html", "web3.html",
(), (),
@ -17,6 +20,7 @@ pub fn build_page(config: Config) -> String {
page_title: Some("Mylloon".into()), page_title: Some("Mylloon".into()),
page_desc: Some("Coin reculé de l'internet".into()), page_desc: Some("Coin reculé de l'internet".into()),
page_kw: None, page_kw: None,
url,
}, },
) )
} }

View file

@ -18,6 +18,8 @@ pub struct Infos {
pub page_desc: Option<String>, pub page_desc: Option<String>,
/// Keywords /// Keywords
pub page_kw: Option<String>, pub page_kw: Option<String>,
/// URL of the website
pub url: String,
} }
/// Final structure given to template /// Final structure given to template