refactored html

This commit is contained in:
JMARyA 2022-11-22 19:48:16 +01:00
parent d92f4886ab
commit 134da8da5c
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
9 changed files with 188 additions and 125 deletions

View file

@ -2,6 +2,7 @@ use crate::{config, pages};
use actix_web::http::header;
use actix_web::web::Form;
use actix_web::*;
use maud::{html, PreEscaped};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
@ -20,45 +21,45 @@ pub async fn message_post(r: HttpRequest, f: Form<MessageForm>) -> impl Responde
config.clone(),
)
.await;
return HttpResponse::Found()
.append_header(("Location", "/message"))
.finish();
return web_base::func::redirect("/message");
}
#[get("/message")]
pub async fn message_page(r: HttpRequest) -> impl Responder {
let config: &web::Data<config::Config> = r.app_data().unwrap();
let host = web_base::func::get_host(&r);
let resp = format!(
r#"
<div class="container" style="margin-top: 25px"><h1>Message</h1>
<br>
<form action="http://{host}/message" method="post" autocomplete="off">
<input value="" type="text" required name="msg_name" placeholder="Name" class="form-control bg-dark text-white" style="margin-bottom: 15px">
<textarea placeholder="Message" required name="message" cols="10" rows="10" class="form-control bg-dark text-white" style="margin-bottom: 15px;">
</textarea>
<input value="Send Message" type="submit" required name="submit" class="btn btn-danger text-white text-decoration-none">
</form>
</div>
"#
);
return pages::html_fn::build_site(resp, "Message", false, true, config).await;
let resp = html! {
div class="container" style="margin-top: 25px" {
h1 { "Message" };
br;
form action=(format!("http://{host}")) method="post" autocomplete="off" {
input value="" type="text" required name="msg_name" placeholder="Name" class="form-control bg-dark text-white" style="margin-bottom: 15px";
textarea placeholder="Message" required name="message" cols="10" rows="10" class="form-control bg-dark text-white" style="margin-bottom: 15px;";
input value="Send Message" type="submit" required name="submit" class="btn btn-danger text-white text-decoration-none";
}
}
};
return pages::html_fn::build_site(resp.into_string(), "Message", false, true, config).await;
}
#[get("/mirrors.txt")]
pub async fn mirrors(r: HttpRequest) -> impl Responder {
let config: &web::Data<config::Config> = r.app_data().unwrap();
if let Ok(mirror_file) = std::fs::File::open("/config/mirrors.txt") {
if let Ok(mirror_file) = std::fs::File::open("./config/mirrors.txt") {
let content = std::io::read_to_string(mirror_file).unwrap();
if web_base::func::is_browser(&r) {
let resp = format!(
r#"
<div style="margin: 25px;">
<pre> {content} </pre>
</div>
"#
);
return pages::html_fn::build_site(resp, "Mirrors", false, true, config).await;
let resp = html! {
div style="margin: 25px;" {
pre {
(content)
};
}
};
return pages::html_fn::build_site(resp.into_string(), "Mirrors", false, true, config)
.await;
}
let res: HttpResponse<String> = HttpResponse::Ok().message_body(content).unwrap();
return res;
@ -74,28 +75,30 @@ pub async fn public_key(r: HttpRequest) -> impl Responder {
if web_base::func::is_browser(&r) {
let config: &web::Data<config::Config> = r.app_data().unwrap();
let host = format!("http://{}", web_base::func::get_host(&r));
let key = std::io::read_to_string(std::fs::File::open("/config/pub.key").unwrap()).unwrap();
let key =
std::io::read_to_string(std::fs::File::open("./config/pub.key").unwrap()).unwrap();
let pgp = gnupg::GnuPG::new().unwrap();
let key_name = pgp.import_key(&key).unwrap().name;
let key = key.replace("\n", "<br>");
let resp = format!(
r#"
<div class="container" style="margin-top: 25px">
<div class="alert alert-info">
<b>To Import: </b>
<span style="display: block;font-family: monospace,monospace;margin-top: 10px; font-size: 20px;overflow-wrap: break-word;">
curl -sL "{host}/public_key"|gpg --import</span>
</div>
<h4 class="container card" style="padding-top: 10px; padding-bottom: 10px; background: black; margin-bottom: 15px;"> {key_name} </h4>
</div>
<div class="container card bg-primary"><p> {key} </p></div>
"#
);
return pages::html_fn::build_site(resp, "Public Key", true, false, config).await;
let resp = html! {
div class="container" style="margin-top: 25px" {
div class="alert alert-info" {
b { "To Import: " };
span style="display: block;font-family: monospace,monospace;margin-top: 10px; font-size: 20px;overflow-wrap: break-word;" { (format!("curl -sL \"{host}/public_key\"|gpg --import")) };
};
h4 class="container card" style="padding-top: 10px; padding-bottom: 10px; background: black; margin-bottom: 15px;" { (key_name) };
};
div class="container card bg-primary" {
p { (PreEscaped(key)) }
}
};
return pages::html_fn::build_site(resp.into_string(), "Public Key", true, false, config)
.await;
}
if let Ok(key_f) = std::fs::File::open("/config/pub.key") {
if let Ok(key_f) = std::fs::File::open("./config/pub.key") {
if let Ok(key_data) = std::io::read_to_string(key_f) {
let res: HttpResponse<String> = HttpResponse::Ok()
.insert_header(header::ContentType::plaintext())
@ -113,42 +116,43 @@ curl -sL "{host}/public_key"|gpg --import</span>
fn build_information_block(conf: &web::Data<config::Config>) -> String {
let name = conf.name().unwrap();
format!(
r#"
<div class="container border-dark" style="margin-top: 20px">
<img src="/assets/me" height=200 width=200 alt="Me" class="rounded">
<br><br>
<h1> {name} </h1>
<hr>
</div>
"#
)
return html! {
div class="container border-dark" style="margin-top: 20px" {
img src="/assets/me" height="200" width="200" alt="Me" class="rounded";
br;br;
h1 { (name) };
hr;
}
}
.into_string();
}
fn build_contact_block(conf: &web::Data<config::Config>) -> String {
if let Some(email) = conf.email() {
let pgp_key_message = match std::path::Path::new("/config/pub.key").exists() {
true => {
r#"
<a href="/public_key"> My PGP Key </a>
<br>
<a href="/message"> Write a message </a>
<br><br>
"#
let pgp_key_message = match std::path::Path::new("./config/pub.key").exists() {
true => html! {
a href="/public_key" { "My PGP Key" };
br;
a href="/message" { "Write a message" };
br;br;
}
false => "",
.into_string(),
false => "".to_string(),
};
return format!(
r#"
<div class="container border-dark">
<h1> <span class="bi bi-person-lines-fill" style="vertical-align: middle;"> </span> Contact </h1>
<hr>
{pgp_key_message}
<a href="mailto:{email}"> {email} </a>
<hr>
</div>
"#
);
return html! {
div class="container border-dark" {
h1 {
span class="bi bi-person-lines-fill" style="vertical-align: middle;";
span { "Contact" };
};
hr;
(PreEscaped(pgp_key_message));
a href=(format!("mailto:{email}")) { (email) };
hr;
}
}
.into_string();
} else {
return "".to_string();
}
@ -156,15 +160,20 @@ fn build_contact_block(conf: &web::Data<config::Config>) -> String {
fn build_donation_block(conf: &web::Data<config::Config>) -> String {
if let Some(xmr_addr) = conf.xmr_address() {
format!(
r#"
<div class="container" style="margin-top: 20px">
<h1> <span class="bi bi-cash-coin"> </span> Donation </h1>
<hr>
<p> <b> Monero: </b> <span style="color: orange;overflow-wrap: break-word;"> {xmr_addr} </span> </p>
</div>
"#
)
html! {
div class="container" style="margin-top: 20px" {
h1 {
span class="bi bi-cash-coin";
span { "Donation" };
};
hr;
p {
b { "Monero: " };
span style="color: orange;overflow-wrap: break-word;" { (xmr_addr) };
}
}
}
.into_string()
} else {
return "".to_string();
}