From fe0ccd913ce727d6902eea11e5a293618947218d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 24 Oct 2023 11:50:30 +0200 Subject: [PATCH] fix url --- Documentation.md | 5 ++++- src/config.rs | 7 +++++- src/misc/utils.rs | 15 ++++++++----- src/routes/agreements.rs | 15 +++++-------- src/routes/blog.rs | 47 +++++++++++----------------------------- src/routes/contact.rs | 15 ++++--------- src/routes/contrib.rs | 15 +++++-------- src/routes/index.rs | 13 ++++------- src/routes/not_found.rs | 13 +++++------ src/routes/portfolio.rs | 17 +++++---------- src/routes/web3.rs | 14 +++++------- src/template.rs | 6 ++--- 12 files changed, 70 insertions(+), 112 deletions(-) diff --git a/Documentation.md b/Documentation.md index 8d51402..59e38d3 100644 --- a/Documentation.md +++ b/Documentation.md @@ -95,10 +95,13 @@ onion = "http://youraddress.onion/" This file is stored at `/app/config/config.toml` ```toml +scheme = "https" # http or https (fallback to 'http' if none) +domain = "sub.domain.tld" # your domain (fallback to 'localhost' if none) +port = 8080 # port used (fallback to '8080' if none) mail = "your.mail at host.com" lang = "lang" onion = "http://youraddress.onion/" -app_name = "Nickname" +app_name = "Nickname" # fallback to 'EWP' if none name = "Firstname" fullname = "Fullname" ``` diff --git a/src/config.rs b/src/config.rs index e3d6b1e..7f2fce0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -11,13 +11,15 @@ use crate::template::Template; pub struct FileConfig { /// http/https pub scheme: Option, + /// Domain name "sub.domain.tld" + pub domain: Option, /// Port used pub port: Option, /// Mail of owner pub mail: Option, /// Lang used pub lang: Option, - /// Adress .onion for Tor + /// .onion address for Tor of the app pub onion: Option, /// App name pub app_name: Option, @@ -32,6 +34,7 @@ impl FileConfig { fn new() -> Self { Self { scheme: Some("http".into()), + domain: Some("localhost".into()), port: Some(8080), app_name: Some("EWP".into()), ..FileConfig::default() @@ -57,6 +60,7 @@ impl FileConfig { port: test(a.port, d.port), mail: test(a.mail, d.mail), lang: test(a.lang, d.lang), + domain: test(a.domain, d.domain), onion: test(a.onion, d.onion), app_name: test(a.app_name, d.app_name), name: test(a.name, d.name), @@ -110,6 +114,7 @@ pub fn get_config(file_path: &str) -> Config { tmpl: Template { directory: format!("{}/{}", files_root, templates_dir), app_name: internal_config.app_name.unwrap(), + url: internal_config.domain.unwrap(), }, } } diff --git a/src/misc/utils.rs b/src/misc/utils.rs index 9c32c91..162b4e8 100644 --- a/src/misc/utils.rs +++ b/src/misc/utils.rs @@ -1,9 +1,8 @@ -use std::cell::Ref; - -use actix_web::dev::ConnectionInfo; use cached::proc_macro::cached; use reqwest::Client; +use crate::config::FileConfig; + #[cached] pub fn get_reqwest_client() -> Client { Client::builder() @@ -13,6 +12,12 @@ pub fn get_reqwest_client() -> Client { } /// Get URL of the app -pub fn get_url(info: Ref<'_, ConnectionInfo>) -> String { - format!("{}://{}", info.scheme(), info.host()) +pub fn get_url(fc: FileConfig) -> String { + /* let port = match fc.scheme.as_deref() { + Some("https") if fc.port == Some(443) => String::new(), + Some("http") if fc.port == Some(80) => String::new(), + _ => format!(":{}", fc.port.unwrap()), + }; */ + + format!("{}://{}", fc.scheme.unwrap(), fc.domain.unwrap()) } diff --git a/src/routes/agreements.rs b/src/routes/agreements.rs index 857dd0e..d26bd81 100644 --- a/src/routes/agreements.rs +++ b/src/routes/agreements.rs @@ -1,33 +1,30 @@ use crate::{config::Config, misc::utils::get_url, template::Infos}; -use actix_web::{get, routes, web, HttpRequest, HttpResponse, Responder}; +use actix_web::{get, routes, web, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; #[routes] #[get("/.well-known/security.txt")] #[get("/security.txt")] -async fn security(req: HttpRequest, config: web::Data) -> impl Responder { - HttpResponse::Ok().body(build_securitytxt( - config.get_ref().to_owned(), - get_url(req.connection_info()), - )) +async fn security(config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_securitytxt(config.get_ref().to_owned())) } #[derive(Content, Debug)] struct SecurityTemplate { + url: String, contact: String, pref_lang: String, - url: String, } #[once(time = 60)] -fn build_securitytxt(config: Config, url: String) -> String { +fn build_securitytxt(config: Config) -> String { config.tmpl.render( "security.txt", SecurityTemplate { + url: format!("{}/.well-known/security.txt", get_url(config.fc.clone())), contact: config.fc.mail.unwrap_or_default(), pref_lang: config.fc.lang.unwrap_or_default(), - url: format!("{}/.well-known/security.txt", url), }, Infos::default(), ) diff --git a/src/routes/blog.rs b/src/routes/blog.rs index c16ff18..30f8af4 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -7,7 +7,7 @@ use ::rss::{ extension::atom::{AtomExtension, Link}, Category, Channel, Guid, Image, Item, }; -use actix_web::{dev::ConnectionInfo, get, web, HttpRequest, HttpResponse, Responder}; +use actix_web::{get, web, HttpResponse, Responder}; use cached::proc_macro::once; use chrono::{DateTime, Datelike, Local, NaiveDateTime, Utc}; use chrono_tz::Europe; @@ -29,11 +29,8 @@ use crate::{ const MIME_TYPE_RSS: &str = "application/rss+xml"; #[get("/blog")] -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()), - )) +async fn index(config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_index(config.get_ref().to_owned())) } #[derive(Content, Debug)] @@ -44,7 +41,7 @@ struct BlogIndexTemplate { } #[once(time = 60)] -fn build_index(config: Config, url: String) -> String { +fn build_index(config: Config) -> String { let mut posts = get_posts("data/blog"); // Sort from newest to oldest @@ -68,7 +65,6 @@ fn build_index(config: Config, url: String) -> String { config.fc.name.unwrap_or_default() )), page_kw: Some(["blog", "blogging"].join(", ")), - url, }, ) } @@ -188,21 +184,13 @@ struct BlogPostTemplate { } #[get("/blog/p/{id}")] -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()), - )) +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())) } -fn build_post(file: String, config: Config, url: String) -> String { +fn build_post(file: String, config: Config) -> String { let mut post = None; - let (infos, toc) = get_post(&mut post, file, config.fc.name.unwrap_or_default(), url); + let (infos, toc) = get_post(&mut post, file, config.fc.name.unwrap_or_default()); config.tmpl.render( "blog/post.html", @@ -218,12 +206,7 @@ fn build_post(file: String, config: Config, url: String) -> String { ) } -fn get_post( - post: &mut Option, - filename: String, - name: String, - url: String, -) -> (Infos, String) { +fn get_post(post: &mut Option, filename: String, name: String) -> (Infos, String) { let blog_dir = "data/blog"; let ext = ".md"; @@ -264,24 +247,20 @@ fn get_post( .collect::>() .join(", "), ), - url, }, toc, ) } #[get("/blog/rss")] -async fn rss(req: HttpRequest, config: web::Data) -> impl Responder { +async fn rss(config: web::Data) -> impl Responder { HttpResponse::Ok() .append_header(("content-type", MIME_TYPE_RSS)) - .body(build_rss( - config.get_ref().to_owned(), - req.connection_info().to_owned(), - )) + .body(build_rss(config.get_ref().to_owned())) } #[once(time = 10800)] // 3h -fn build_rss(config: Config, info: ConnectionInfo) -> String { +fn build_rss(config: Config) -> String { let mut posts = get_posts("data/blog"); // Sort from newest to oldest @@ -294,7 +273,7 @@ fn build_rss(config: Config, info: ConnectionInfo) -> String { posts.drain(max..); } - let link_to_site = format!("{}://{}", info.scheme(), info.host()); + let link_to_site = get_url(config.fc.clone()); let author = if let (Some(mail), Some(name)) = (config.fc.mail, config.fc.fullname.to_owned()) { Some(format!("{mail} ({name})")) } else { diff --git a/src/routes/contact.rs b/src/routes/contact.rs index 33f0973..d71db65 100644 --- a/src/routes/contact.rs +++ b/src/routes/contact.rs @@ -6,10 +6,7 @@ use std::fs::read_to_string; use crate::{ config::Config, - misc::{ - markdown::{read_file, File, TypeFileMetadata}, - utils::get_url, - }, + misc::markdown::{read_file, File, TypeFileMetadata}, template::{Infos, NavBar}, }; @@ -26,11 +23,8 @@ pub fn pages(cfg: &mut web::ServiceConfig) { } #[get("")] -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()), - )) +async fn page(config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) } #[derive(Clone, Debug)] @@ -125,7 +119,7 @@ fn remove_paragraphs(list: &mut [File]) { } #[once(time = 60)] -fn build_page(config: Config, url: String) -> String { +fn build_page(config: Config) -> String { let contacts_dir = "data/contacts"; let ext = ".md"; @@ -172,7 +166,6 @@ fn build_page(config: Config, url: String) -> String { page_title: Some("Contacts".into()), page_desc: Some(format!("Réseaux d'{}", config.fc.name.unwrap_or_default())), page_kw: None, - url, }, ) } diff --git a/src/routes/contrib.rs b/src/routes/contrib.rs index 5d619aa..90f8e17 100644 --- a/src/routes/contrib.rs +++ b/src/routes/contrib.rs @@ -2,20 +2,16 @@ use std::collections::HashMap; use crate::{ config::Config, - misc::{ - github::{fetch_pr, ProjectState}, - utils::get_url, - }, + misc::github::{fetch_pr, ProjectState}, template::{Infos, NavBar}, }; -use actix_web::{get, web, HttpRequest, HttpResponse, Responder}; +use actix_web::{get, web, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; #[get("/contrib")] -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) +async fn page(config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_page(config.get_ref().to_owned()).await) } #[derive(Content, Debug)] @@ -46,7 +42,7 @@ struct Pull { } #[once(time = 600)] // 10min -async fn build_page(config: Config, url: String) -> String { +async fn build_page(config: Config) -> String { let navbar = NavBar { contrib: true, ..NavBar::default() @@ -158,7 +154,6 @@ async fn build_page(config: Config, url: String) -> String { config.fc.name.unwrap_or_default() )), page_kw: None, - url, }, ) } diff --git a/src/routes/index.rs b/src/routes/index.rs index c3f3dce..c8cd0e8 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -1,19 +1,15 @@ -use actix_web::{get, web, HttpRequest, HttpResponse, Responder}; +use actix_web::{get, web, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; use crate::{ config::Config, - misc::utils::get_url, template::{Infos, NavBar}, }; #[get("/")] -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()), - )) +async fn page(config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) } #[derive(Content, Debug)] @@ -23,7 +19,7 @@ struct IndexTemplate { } #[once(time = 60)] -fn build_page(config: Config, url: String) -> String { +fn build_page(config: Config) -> String { config.tmpl.render( "index.html", IndexTemplate { @@ -41,7 +37,6 @@ fn build_page(config: Config, url: String) -> String { page_title: config.fc.fullname, page_desc: Some("Page principale".into()), page_kw: None, - url, }, ) } diff --git a/src/routes/not_found.rs b/src/routes/not_found.rs index 51c7f69..c257142 100644 --- a/src/routes/not_found.rs +++ b/src/routes/not_found.rs @@ -1,4 +1,4 @@ -use actix_web::{web, HttpRequest, HttpResponse, Responder}; +use actix_web::{web, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; @@ -8,11 +8,8 @@ use crate::{ template::{Infos, NavBar}, }; -pub async fn page(req: HttpRequest, config: web::Data) -> impl Responder { - HttpResponse::NotFound().body(build_page( - config.get_ref().to_owned(), - get_url(req.connection_info()), - )) +pub async fn page(config: web::Data) -> impl Responder { + HttpResponse::NotFound().body(build_page(config.get_ref().to_owned())) } #[derive(Content, Debug)] @@ -23,12 +20,12 @@ struct NotFoundTemplate { } #[once(time = 60)] -fn build_page(config: Config, url: String) -> String { +fn build_page(config: Config) -> String { config.tmpl.render( "404.html", NotFoundTemplate { navbar: NavBar::default(), - www: url, + www: get_url(config.fc.clone()), onion: config.fc.onion, }, Infos { diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index 953df52..13d3b52 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -1,23 +1,17 @@ -use actix_web::{get, web, HttpRequest, HttpResponse, Responder}; +use actix_web::{get, web, HttpResponse, Responder}; use cached::proc_macro::once; use glob::glob; use ramhorns::Content; use crate::{ config::Config, - misc::{ - markdown::{read_file, File, TypeFileMetadata}, - utils::get_url, - }, + misc::markdown::{read_file, File, TypeFileMetadata}, template::{Infos, NavBar}, }; #[get("/portfolio")] -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()), - )) +async fn page(config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) } #[derive(Content, Debug)] @@ -31,7 +25,7 @@ struct PortfolioTemplate<'a> { } #[once(time = 60)] -fn build_page(config: Config, url: String) -> String { +fn build_page(config: Config) -> String { let projects_dir = "data/projects"; let ext = ".md"; @@ -79,7 +73,6 @@ fn build_page(config: Config, url: String) -> String { config.fc.name.unwrap_or_default() )), page_kw: None, - url, }, ) } diff --git a/src/routes/web3.rs b/src/routes/web3.rs index 6d9cb0b..32e9e33 100644 --- a/src/routes/web3.rs +++ b/src/routes/web3.rs @@ -1,18 +1,15 @@ -use actix_web::{get, web, HttpRequest, HttpResponse, Responder}; +use actix_web::{get, web, HttpResponse, Responder}; use cached::proc_macro::once; -use crate::{config::Config, misc::utils::get_url, template::Infos}; +use crate::{config::Config, template::Infos}; #[get("/web3")] -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()), - )) +async fn page(config: web::Data) -> impl Responder { + HttpResponse::Ok().body(build_page(config.get_ref().to_owned())) } #[once] -fn build_page(config: Config, url: String) -> String { +fn build_page(config: Config) -> String { config.tmpl.render( "web3.html", (), @@ -20,7 +17,6 @@ fn build_page(config: Config, url: String) -> 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 e01279a..bad8315 100644 --- a/src/template.rs +++ b/src/template.rs @@ -7,6 +7,8 @@ pub struct Template { pub directory: String, /// App name pub app_name: String, + /// URL of app + pub url: String, } /// Structure used by /routes/*.rs @@ -18,8 +20,6 @@ pub struct Infos { pub page_desc: Option, /// Keywords pub page_kw: Option, - /// URL of the website - pub url: String, } #[derive(Content, Debug, Default)] @@ -64,7 +64,7 @@ impl Template { page_title: info.page_title, page_desc: info.page_desc.map(add_quotes), page_kw: info.page_kw.map(add_quotes), - url: add_quotes(info.url), + url: add_quotes(self.url.to_owned()), data, }) }