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 github;
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 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())
}

View file

@ -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<Config>) -> 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(),
)

View file

@ -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<Config>) -> impl Responder {
HttpResponse::Ok().body(build_index(config.get_ref().to_owned()))
pub async fn index(req: HttpRequest, config: web::Data<Config>) -> 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<Config>) -> 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<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 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<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 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_desc: Some(format!("Blog d'{name}")),
page_kw: Some(["blog", "blogging", "write", "writing"].join(", ")),
url,
}
}

View file

@ -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<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page(config.get_ref().to_owned()).await)
pub async fn page(req: HttpRequest, config: web::Data<Config>) -> 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,
},
)
}

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 crate::{config::Config, template::Infos};
use crate::{config::Config, misc::utils::get_url, template::Infos};
#[get("/")]
pub async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page(config.get_ref().to_owned()))
pub async fn page(req: HttpRequest, config: web::Data<Config>) -> 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,
},
)
}

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 crate::{config::Config, template::Infos};
use crate::{config::Config, misc::utils::get_url, template::Infos};
#[get("/networks")]
pub async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page(config.get_ref().to_owned()))
pub async fn page(req: HttpRequest, config: web::Data<Config>) -> 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,
},
)
}

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 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<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page(config.get_ref().to_owned()))
pub async fn page(req: HttpRequest, config: web::Data<Config>) -> 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,
},
)
}

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 crate::{config::Config, template::Infos};
use crate::{config::Config, misc::utils::get_url, template::Infos};
#[get("/web3")]
pub async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page(config.get_ref().to_owned()))
pub async fn page(req: HttpRequest, config: web::Data<Config>) -> 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,
},
)
}

View file

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