refactor
This commit is contained in:
parent
5895bb7d56
commit
f2b24d33e1
7 changed files with 70 additions and 56 deletions
src/pages
|
@ -1,7 +1,7 @@
|
|||
use crate::{config, pages};
|
||||
use actix_web::http::header;
|
||||
use actix_web::web::Form;
|
||||
use actix_web::*;
|
||||
use actix_web::{get, post, web, Error, HttpRequest, HttpResponse, Responder, Result};
|
||||
use maud::{html, PreEscaped};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
|
@ -13,8 +13,8 @@ pub struct MessageForm {
|
|||
|
||||
#[post("/message")]
|
||||
pub async fn message_post(r: HttpRequest, f: Form<MessageForm>) -> impl Responder {
|
||||
let config: &web::Data<config::Config> = r.app_data().unwrap();
|
||||
crate::msg::save_msg(f.message.clone(), &f.msg_name.to_string());
|
||||
let config: &web::Data<config::Config> = r.app_data().expect("get config failed");
|
||||
crate::msg::save_message(f.message.clone(), &f.msg_name.to_string());
|
||||
crate::notification::notify(
|
||||
&format!("New Message from {}", f.msg_name),
|
||||
"New Message",
|
||||
|
@ -26,7 +26,7 @@ pub async fn message_post(r: HttpRequest, f: Form<MessageForm>) -> impl Responde
|
|||
|
||||
#[get("/message")]
|
||||
pub async fn message_page(r: HttpRequest) -> impl Responder {
|
||||
let config: &web::Data<config::Config> = r.app_data().unwrap();
|
||||
let config: &web::Data<config::Config> = r.app_data().expect("get config failed");
|
||||
|
||||
let resp = html! {
|
||||
div class="container" style="margin-top: 25px" {
|
||||
|
@ -45,10 +45,10 @@ pub async fn message_page(r: HttpRequest) -> impl Responder {
|
|||
}
|
||||
|
||||
#[get("/mirrors.txt")]
|
||||
pub async fn mirrors(r: HttpRequest) -> impl Responder {
|
||||
let config: &web::Data<config::Config> = r.app_data().unwrap();
|
||||
pub async fn mirrors(r: HttpRequest) -> Result<impl Responder, Error> {
|
||||
let config: &web::Data<config::Config> = r.app_data().expect("get config failed");
|
||||
if let Ok(mirror_file) = std::fs::File::open("./config/mirrors.txt") {
|
||||
let content = std::io::read_to_string(mirror_file).unwrap();
|
||||
let content = std::io::read_to_string(mirror_file).expect("could not read file");
|
||||
if web_base::func::is_browser(&r) {
|
||||
let resp = html! {
|
||||
div style="margin: 25px;" {
|
||||
|
@ -57,26 +57,32 @@ pub async fn mirrors(r: HttpRequest) -> impl Responder {
|
|||
};
|
||||
}
|
||||
};
|
||||
return pages::html_fn::build_site(resp.into_string(), "Mirrors", false, true, config)
|
||||
.await;
|
||||
return Ok(pages::html_fn::build_site(
|
||||
resp.into_string(),
|
||||
"Mirrors",
|
||||
false,
|
||||
true,
|
||||
config,
|
||||
)
|
||||
.await);
|
||||
}
|
||||
return HttpResponse::Ok().message_body(content).unwrap();
|
||||
return HttpResponse::Ok().message_body(content);
|
||||
}
|
||||
HttpResponse::NotFound()
|
||||
.message_body("".to_string())
|
||||
.unwrap()
|
||||
HttpResponse::NotFound().message_body(String::new())
|
||||
}
|
||||
|
||||
#[get("/public_key")]
|
||||
pub async fn public_key(r: HttpRequest) -> impl Responder {
|
||||
pub async fn public_key(r: HttpRequest) -> Result<impl Responder, Error> {
|
||||
if web_base::func::is_browser(&r) {
|
||||
let config: &web::Data<config::Config> = r.app_data().unwrap();
|
||||
let config: &web::Data<config::Config> = r.app_data().expect("get config failed");
|
||||
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").expect("key could not be opened"),
|
||||
)
|
||||
.expect("could not read key");
|
||||
|
||||
let pgp = gnupg::GnuPG::new().unwrap();
|
||||
let key_name = pgp.import_key(&key).unwrap().name;
|
||||
let pgp = gnupg::GnuPG::new().expect("no gpg");
|
||||
let key_name = pgp.import_key(&key).expect("key not valid").name;
|
||||
let key = key.replace('\n', "<br>");
|
||||
|
||||
let resp = html! {
|
||||
|
@ -92,25 +98,28 @@ pub async fn public_key(r: HttpRequest) -> impl Responder {
|
|||
}
|
||||
};
|
||||
|
||||
return pages::html_fn::build_site(resp.into_string(), "Public Key", true, false, config)
|
||||
.await;
|
||||
return Ok(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_data) = std::io::read_to_string(key_f) {
|
||||
return HttpResponse::Ok()
|
||||
.insert_header(header::ContentType::plaintext())
|
||||
.message_body(key_data)
|
||||
.unwrap();
|
||||
.message_body(key_data);
|
||||
}
|
||||
}
|
||||
|
||||
HttpResponse::NotFound()
|
||||
.message_body("".to_string())
|
||||
.unwrap()
|
||||
HttpResponse::NotFound().message_body(String::new())
|
||||
}
|
||||
|
||||
fn build_information_block(conf: &web::Data<config::Config>) -> String {
|
||||
let name = conf.name().unwrap();
|
||||
let name = conf.name().expect("no name found");
|
||||
html! {
|
||||
div class="container border-dark" style="margin-top: 20px" {
|
||||
img src="/assets/me" height="200" width="200" alt="Me" class="rounded";
|
||||
|
@ -124,15 +133,16 @@ fn build_information_block(conf: &web::Data<config::Config>) -> 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 => html! {
|
||||
let pgp_key_message = if std::path::Path::new("./config/pub.key").exists() {
|
||||
html! {
|
||||
a href="/public_key" { "My PGP Key" };
|
||||
br;
|
||||
a href="/message" { "Write a message" };
|
||||
br;br;
|
||||
}
|
||||
.into_string(),
|
||||
false => "".to_string(),
|
||||
.into_string()
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
html! {
|
||||
|
@ -149,7 +159,7 @@ fn build_contact_block(conf: &web::Data<config::Config>) -> String {
|
|||
}
|
||||
.into_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -170,7 +180,7 @@ fn build_donation_block(conf: &web::Data<config::Config>) -> String {
|
|||
}
|
||||
.into_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue