fix url
All checks were successful
ci/woodpecker/push/publish Pipeline was successful

This commit is contained in:
Mylloon 2023-10-24 11:50:30 +02:00
parent cea27a6af9
commit fe0ccd913c
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
12 changed files with 70 additions and 112 deletions

View file

@ -95,10 +95,13 @@ onion = "http://youraddress.onion/"
This file is stored at `/app/config/config.toml` This file is stored at `/app/config/config.toml`
```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" mail = "your.mail at host.com"
lang = "lang" lang = "lang"
onion = "http://youraddress.onion/" onion = "http://youraddress.onion/"
app_name = "Nickname" app_name = "Nickname" # fallback to 'EWP' if none
name = "Firstname" name = "Firstname"
fullname = "Fullname" fullname = "Fullname"
``` ```

View file

@ -11,13 +11,15 @@ use crate::template::Template;
pub struct FileConfig { pub struct FileConfig {
/// http/https /// http/https
pub scheme: Option<String>, pub scheme: Option<String>,
/// Domain name "sub.domain.tld"
pub domain: Option<String>,
/// Port used /// Port used
pub port: Option<u16>, pub port: Option<u16>,
/// Mail of owner /// Mail of owner
pub mail: Option<String>, pub mail: Option<String>,
/// Lang used /// Lang used
pub lang: Option<String>, pub lang: Option<String>,
/// Adress .onion for Tor /// .onion address for Tor of the app
pub onion: Option<String>, pub onion: Option<String>,
/// App name /// App name
pub app_name: Option<String>, pub app_name: Option<String>,
@ -32,6 +34,7 @@ impl FileConfig {
fn new() -> Self { fn new() -> Self {
Self { Self {
scheme: Some("http".into()), scheme: Some("http".into()),
domain: Some("localhost".into()),
port: Some(8080), port: Some(8080),
app_name: Some("EWP".into()), app_name: Some("EWP".into()),
..FileConfig::default() ..FileConfig::default()
@ -57,6 +60,7 @@ impl FileConfig {
port: test(a.port, d.port), port: test(a.port, d.port),
mail: test(a.mail, d.mail), mail: test(a.mail, d.mail),
lang: test(a.lang, d.lang), lang: test(a.lang, d.lang),
domain: test(a.domain, d.domain),
onion: test(a.onion, d.onion), onion: test(a.onion, d.onion),
app_name: test(a.app_name, d.app_name), app_name: test(a.app_name, d.app_name),
name: test(a.name, d.name), name: test(a.name, d.name),
@ -110,6 +114,7 @@ pub fn get_config(file_path: &str) -> Config {
tmpl: Template { tmpl: Template {
directory: format!("{}/{}", files_root, templates_dir), directory: format!("{}/{}", files_root, templates_dir),
app_name: internal_config.app_name.unwrap(), app_name: internal_config.app_name.unwrap(),
url: internal_config.domain.unwrap(),
}, },
} }
} }

View file

@ -1,9 +1,8 @@
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;
use crate::config::FileConfig;
#[cached] #[cached]
pub fn get_reqwest_client() -> Client { pub fn get_reqwest_client() -> Client {
Client::builder() Client::builder()
@ -13,6 +12,12 @@ pub fn get_reqwest_client() -> Client {
} }
/// Get URL of the app /// Get URL of the app
pub fn get_url(info: Ref<'_, ConnectionInfo>) -> String { pub fn get_url(fc: FileConfig) -> String {
format!("{}://{}", info.scheme(), info.host()) /* 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())
} }

View file

@ -1,33 +1,30 @@
use crate::{config::Config, misc::utils::get_url, template::Infos}; 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 cached::proc_macro::once;
use ramhorns::Content; use ramhorns::Content;
#[routes] #[routes]
#[get("/.well-known/security.txt")] #[get("/.well-known/security.txt")]
#[get("/security.txt")] #[get("/security.txt")]
async fn security(req: HttpRequest, config: web::Data<Config>) -> impl Responder { async fn security(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(),
get_url(req.connection_info()),
))
} }
#[derive(Content, Debug)] #[derive(Content, Debug)]
struct SecurityTemplate { struct SecurityTemplate {
url: String,
contact: String, contact: String,
pref_lang: String, pref_lang: String,
url: String,
} }
#[once(time = 60)] #[once(time = 60)]
fn build_securitytxt(config: Config, url: String) -> String { fn build_securitytxt(config: Config) -> String {
config.tmpl.render( config.tmpl.render(
"security.txt", "security.txt",
SecurityTemplate { SecurityTemplate {
url: format!("{}/.well-known/security.txt", get_url(config.fc.clone())),
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!("{}/.well-known/security.txt", url),
}, },
Infos::default(), Infos::default(),
) )

View file

@ -7,7 +7,7 @@ use ::rss::{
extension::atom::{AtomExtension, Link}, extension::atom::{AtomExtension, Link},
Category, Channel, Guid, Image, Item, 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 cached::proc_macro::once;
use chrono::{DateTime, Datelike, Local, NaiveDateTime, Utc}; use chrono::{DateTime, Datelike, Local, NaiveDateTime, Utc};
use chrono_tz::Europe; use chrono_tz::Europe;
@ -29,11 +29,8 @@ use crate::{
const MIME_TYPE_RSS: &str = "application/rss+xml"; const MIME_TYPE_RSS: &str = "application/rss+xml";
#[get("/blog")] #[get("/blog")]
async fn index(req: HttpRequest, config: web::Data<Config>) -> impl Responder { async fn index(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_index( HttpResponse::Ok().body(build_index(config.get_ref().to_owned()))
config.get_ref().to_owned(),
get_url(req.connection_info()),
))
} }
#[derive(Content, Debug)] #[derive(Content, Debug)]
@ -44,7 +41,7 @@ struct BlogIndexTemplate {
} }
#[once(time = 60)] #[once(time = 60)]
fn build_index(config: Config, url: String) -> String { fn build_index(config: Config) -> 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
@ -68,7 +65,6 @@ fn build_index(config: Config, url: String) -> 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,
}, },
) )
} }
@ -188,21 +184,13 @@ struct BlogPostTemplate {
} }
#[get("/blog/p/{id}")] #[get("/blog/p/{id}")]
async fn page( async fn page(path: web::Path<(String,)>, config: web::Data<Config>) -> impl Responder {
req: HttpRequest, HttpResponse::Ok().body(build_post(path.into_inner().0, config.get_ref().to_owned()))
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, url: String) -> String { fn build_post(file: String, config: Config) -> String {
let mut post = None; 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( config.tmpl.render(
"blog/post.html", "blog/post.html",
@ -218,12 +206,7 @@ fn build_post(file: String, config: Config, url: String) -> String {
) )
} }
fn get_post( fn get_post(post: &mut Option<File>, filename: String, name: String) -> (Infos, String) {
post: &mut Option<File>,
filename: String,
name: String,
url: String,
) -> (Infos, String) {
let blog_dir = "data/blog"; let blog_dir = "data/blog";
let ext = ".md"; let ext = ".md";
@ -264,24 +247,20 @@ fn get_post(
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(", "), .join(", "),
), ),
url,
}, },
toc, toc,
) )
} }
#[get("/blog/rss")] #[get("/blog/rss")]
async fn rss(req: HttpRequest, config: web::Data<Config>) -> impl Responder { async fn rss(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok() HttpResponse::Ok()
.append_header(("content-type", MIME_TYPE_RSS)) .append_header(("content-type", MIME_TYPE_RSS))
.body(build_rss( .body(build_rss(config.get_ref().to_owned()))
config.get_ref().to_owned(),
req.connection_info().to_owned(),
))
} }
#[once(time = 10800)] // 3h #[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"); let mut posts = get_posts("data/blog");
// Sort from newest to oldest // Sort from newest to oldest
@ -294,7 +273,7 @@ fn build_rss(config: Config, info: ConnectionInfo) -> String {
posts.drain(max..); 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()) { let author = if let (Some(mail), Some(name)) = (config.fc.mail, config.fc.fullname.to_owned()) {
Some(format!("{mail} ({name})")) Some(format!("{mail} ({name})"))
} else { } else {

View file

@ -6,10 +6,7 @@ use std::fs::read_to_string;
use crate::{ use crate::{
config::Config, config::Config,
misc::{ misc::markdown::{read_file, File, TypeFileMetadata},
markdown::{read_file, File, TypeFileMetadata},
utils::get_url,
},
template::{Infos, NavBar}, template::{Infos, NavBar},
}; };
@ -26,11 +23,8 @@ pub fn pages(cfg: &mut web::ServiceConfig) {
} }
#[get("")] #[get("")]
async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder { async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page( HttpResponse::Ok().body(build_page(config.get_ref().to_owned()))
config.get_ref().to_owned(),
get_url(req.connection_info()),
))
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -125,7 +119,7 @@ fn remove_paragraphs(list: &mut [File]) {
} }
#[once(time = 60)] #[once(time = 60)]
fn build_page(config: Config, url: String) -> String { fn build_page(config: Config) -> String {
let contacts_dir = "data/contacts"; let contacts_dir = "data/contacts";
let ext = ".md"; let ext = ".md";
@ -172,7 +166,6 @@ fn build_page(config: Config, url: String) -> String {
page_title: Some("Contacts".into()), page_title: Some("Contacts".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

@ -2,20 +2,16 @@ use std::collections::HashMap;
use crate::{ use crate::{
config::Config, config::Config,
misc::{ misc::github::{fetch_pr, ProjectState},
github::{fetch_pr, ProjectState},
utils::get_url,
},
template::{Infos, NavBar}, template::{Infos, NavBar},
}; };
use actix_web::{get, web, HttpRequest, HttpResponse, Responder}; use actix_web::{get, web, HttpResponse, Responder};
use cached::proc_macro::once; use cached::proc_macro::once;
use ramhorns::Content; use ramhorns::Content;
#[get("/contrib")] #[get("/contrib")]
async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder { async fn page(config: web::Data<Config>) -> impl Responder {
let url = get_url(req.connection_info()); HttpResponse::Ok().body(build_page(config.get_ref().to_owned()).await)
HttpResponse::Ok().body(build_page(config.get_ref().to_owned(), url).await)
} }
#[derive(Content, Debug)] #[derive(Content, Debug)]
@ -46,7 +42,7 @@ struct Pull {
} }
#[once(time = 600)] // 10min #[once(time = 600)] // 10min
async fn build_page(config: Config, url: String) -> String { async fn build_page(config: Config) -> String {
let navbar = NavBar { let navbar = NavBar {
contrib: true, contrib: true,
..NavBar::default() ..NavBar::default()
@ -158,7 +154,6 @@ async fn build_page(config: Config, url: String) -> String {
config.fc.name.unwrap_or_default() config.fc.name.unwrap_or_default()
)), )),
page_kw: None, page_kw: None,
url,
}, },
) )
} }

View file

@ -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 cached::proc_macro::once;
use ramhorns::Content; use ramhorns::Content;
use crate::{ use crate::{
config::Config, config::Config,
misc::utils::get_url,
template::{Infos, NavBar}, template::{Infos, NavBar},
}; };
#[get("/")] #[get("/")]
async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder { async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page( HttpResponse::Ok().body(build_page(config.get_ref().to_owned()))
config.get_ref().to_owned(),
get_url(req.connection_info()),
))
} }
#[derive(Content, Debug)] #[derive(Content, Debug)]
@ -23,7 +19,7 @@ struct IndexTemplate {
} }
#[once(time = 60)] #[once(time = 60)]
fn build_page(config: Config, url: String) -> String { fn build_page(config: Config) -> String {
config.tmpl.render( config.tmpl.render(
"index.html", "index.html",
IndexTemplate { IndexTemplate {
@ -41,7 +37,6 @@ fn build_page(config: Config, url: String) -> 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,4 +1,4 @@
use actix_web::{web, HttpRequest, HttpResponse, Responder}; use actix_web::{web, HttpResponse, Responder};
use cached::proc_macro::once; use cached::proc_macro::once;
use ramhorns::Content; use ramhorns::Content;
@ -8,11 +8,8 @@ use crate::{
template::{Infos, NavBar}, template::{Infos, NavBar},
}; };
pub async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder { pub async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::NotFound().body(build_page( HttpResponse::NotFound().body(build_page(config.get_ref().to_owned()))
config.get_ref().to_owned(),
get_url(req.connection_info()),
))
} }
#[derive(Content, Debug)] #[derive(Content, Debug)]
@ -23,12 +20,12 @@ struct NotFoundTemplate {
} }
#[once(time = 60)] #[once(time = 60)]
fn build_page(config: Config, url: String) -> String { fn build_page(config: Config) -> String {
config.tmpl.render( config.tmpl.render(
"404.html", "404.html",
NotFoundTemplate { NotFoundTemplate {
navbar: NavBar::default(), navbar: NavBar::default(),
www: url, www: get_url(config.fc.clone()),
onion: config.fc.onion, onion: config.fc.onion,
}, },
Infos { Infos {

View file

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

View file

@ -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 cached::proc_macro::once;
use crate::{config::Config, misc::utils::get_url, template::Infos}; use crate::{config::Config, template::Infos};
#[get("/web3")] #[get("/web3")]
async fn page(req: HttpRequest, config: web::Data<Config>) -> impl Responder { async fn page(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(build_page( HttpResponse::Ok().body(build_page(config.get_ref().to_owned()))
config.get_ref().to_owned(),
get_url(req.connection_info()),
))
} }
#[once] #[once]
fn build_page(config: Config, url: String) -> String { fn build_page(config: Config) -> String {
config.tmpl.render( config.tmpl.render(
"web3.html", "web3.html",
(), (),
@ -20,7 +17,6 @@ fn build_page(config: Config, url: String) -> 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

@ -7,6 +7,8 @@ pub struct Template {
pub directory: String, pub directory: String,
/// App name /// App name
pub app_name: String, pub app_name: String,
/// URL of app
pub url: String,
} }
/// Structure used by /routes/*.rs /// Structure used by /routes/*.rs
@ -18,8 +20,6 @@ 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,
} }
#[derive(Content, Debug, Default)] #[derive(Content, Debug, Default)]
@ -64,7 +64,7 @@ impl Template {
page_title: info.page_title, page_title: info.page_title,
page_desc: info.page_desc.map(add_quotes), page_desc: info.page_desc.map(add_quotes),
page_kw: info.page_kw.map(add_quotes), page_kw: info.page_kw.map(add_quotes),
url: add_quotes(info.url), url: add_quotes(self.url.to_owned()),
data, data,
}) })
} }