From f2b24d33e18d665d5b5ade4e16a949ce5acdc52b Mon Sep 17 00:00:00 2001 From: JMARyA Date: Sat, 28 Jan 2023 07:24:50 +0100 Subject: [PATCH] refactor --- Cargo.lock | 20 ++++++------ src/main.rs | 2 +- src/msg.rs | 16 ++++++---- src/notification.rs | 2 +- src/pages/assets.rs | 2 +- src/pages/html_fn.rs | 8 +++-- src/pages/index.rs | 76 +++++++++++++++++++++++++------------------- 7 files changed, 70 insertions(+), 56 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eedf9d7..0509116 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -445,9 +445,9 @@ dependencies = [ [[package]] name = "cxx" -version = "1.0.87" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b61a7545f753a88bcbe0a70de1fcc0221e10bfc752f576754fa91e663db1622e" +checksum = "322296e2f2e5af4270b54df9e85a02ff037e271af20ba3e7fe1575515dc840b8" dependencies = [ "cc", "cxxbridge-flags", @@ -457,9 +457,9 @@ dependencies = [ [[package]] name = "cxx-build" -version = "1.0.87" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f464457d494b5ed6905c63b0c4704842aba319084a0a3561cdc1359536b53200" +checksum = "017a1385b05d631e7875b1f151c9f012d37b53491e2a87f65bff5c262b2111d8" dependencies = [ "cc", "codespan-reporting", @@ -472,15 +472,15 @@ dependencies = [ [[package]] name = "cxxbridge-flags" -version = "1.0.87" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43c7119ce3a3701ed81aca8410b9acf6fc399d2629d057b87e2efa4e63a3aaea" +checksum = "c26bbb078acf09bc1ecda02d4223f03bdd28bd4874edcb0379138efc499ce971" [[package]] name = "cxxbridge-macro" -version = "1.0.87" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65e07508b90551e610910fa648a1878991d367064997a596135b86df30daf07e" +checksum = "357f40d1f06a24b60ae1fe122542c1fb05d28d32acb2aed064e84bc2ad1e252e" dependencies = [ "proc-macro2", "quote", @@ -644,7 +644,7 @@ dependencies = [ [[package]] name = "gnupg" version = "0.1.0" -source = "git+https://git.hydrar.de/jmarya/gnupg-rs#3b57d7879b8bb676ffc860feac433611230118f6" +source = "git+https://git.hydrar.de/jmarya/gnupg-rs#5e08b746122a6df58ca648bfe57c76bd40f7e5fd" [[package]] name = "h2" @@ -1802,7 +1802,7 @@ checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" [[package]] name = "web-base" version = "0.1.0" -source = "git+https://git.hydrar.de/jmarya/web-base#0920a1fcbc15c4ec5bbf131103e98e44cb2a0968" +source = "git+https://git.hydrar.de/jmarya/web-base#0c6e643cca39e7b18a7b3a870313752c6464c35b" dependencies = [ "actix-files", "actix-web", diff --git a/src/main.rs b/src/main.rs index da4602c..b10801c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ mod pages; use std::sync::Arc; use tokio::sync::Mutex; -use actix_web::*; +use actix_web::{web, App, HttpServer}; #[actix_web::main] async fn main() -> std::io::Result<()> { diff --git a/src/msg.rs b/src/msg.rs index e57b1b6..6f9732e 100644 --- a/src/msg.rs +++ b/src/msg.rs @@ -1,17 +1,19 @@ use std::io::Write; pub fn encrypt(msg: String) -> String { - let pgp = gnupg::GnuPG::new().unwrap(); + let pgp = gnupg::GnuPG::new().expect("no gpg"); let pub_key = pgp - .import_key(&std::fs::read_to_string("./config/pub.key").unwrap()) - .unwrap(); - pgp.encrypt(&pub_key, &msg).unwrap() + .import_key(&std::fs::read_to_string("./config/pub.key").expect("key could not be read")) + .expect("key import error"); + pgp.encrypt(&pub_key, &msg).expect("encryption failed") } -pub fn save_msg(msg: String, name: &str) { +pub fn save_message(msg: String, name: &str) { std::fs::create_dir_all("./data/messages").expect("couldn't create msg dir"); let time = chrono::offset::Utc::now(); let time = time.format("%Y-%m-%d.%H-%M").to_string(); - let mut f = std::fs::File::create(format!("./data/messages/{name}-{time}.asc")).unwrap(); - f.write_all(encrypt(msg).as_bytes()).unwrap(); + let mut f = std::fs::File::create(format!("./data/messages/{name}-{time}.asc")) + .expect("could not create file"); + f.write_all(encrypt(msg).as_bytes()) + .expect("file could not be written"); } diff --git a/src/notification.rs b/src/notification.rs index 73e7367..57227d3 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -1,7 +1,7 @@ use crate::config; use crate::config::Config; use actix_web::web::Data; -use log::*; +use log::info; pub async fn notify(msg: &str, title: &str, config: Data) { if let Some(gotify) = config.gotify_config() { diff --git a/src/pages/assets.rs b/src/pages/assets.rs index 56d857f..10f8e4a 100644 --- a/src/pages/assets.rs +++ b/src/pages/assets.rs @@ -1,5 +1,5 @@ use actix_files::NamedFile; -use actix_web::*; +use actix_web::{get, Result}; // Assets diff --git a/src/pages/html_fn.rs b/src/pages/html_fn.rs index c9d51dc..28d697e 100644 --- a/src/pages/html_fn.rs +++ b/src/pages/html_fn.rs @@ -1,6 +1,6 @@ use crate::config::Config; use actix_web::web::Data; -use actix_web::*; +use actix_web::HttpResponse; use maud::{html, PreEscaped}; pub(crate) async fn build_site( @@ -17,7 +17,7 @@ pub(crate) async fn build_site( }; let mut c_class = "bg-dark text-white justify-content-center text-center".to_string(); - let mut c_style = "".to_string(); + let mut c_style = String::new(); let mut g_style = "a {text-decoration: none; font-weight: bold; color: white}".to_string(); if !disable_color { if let (Some(fg), Some(bg)) = (config.fg_color(), config.bg_color()) { @@ -49,5 +49,7 @@ pub(crate) async fn build_site( }; }; - HttpResponse::Ok().message_body(r.into_string()).unwrap() + HttpResponse::Ok() + .message_body(r.into_string()) + .expect("could not build page") } diff --git a/src/pages/index.rs b/src/pages/index.rs index fdee71f..09df63b 100644 --- a/src/pages/index.rs +++ b/src/pages/index.rs @@ -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) -> impl Responder { - let config: &web::Data = r.app_data().unwrap(); - crate::msg::save_msg(f.message.clone(), &f.msg_name.to_string()); + let config: &web::Data = 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) -> impl Responde #[get("/message")] pub async fn message_page(r: HttpRequest) -> impl Responder { - let config: &web::Data = r.app_data().unwrap(); + let config: &web::Data = 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 = r.app_data().unwrap(); +pub async fn mirrors(r: HttpRequest) -> Result { + let config: &web::Data = 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 { if web_base::func::is_browser(&r) { - let config: &web::Data = r.app_data().unwrap(); + let config: &web::Data = 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', "
"); 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) -> 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) -> String { fn build_contact_block(conf: &web::Data) -> 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) -> String { } .into_string() } else { - "".to_string() + String::new() } } @@ -170,7 +180,7 @@ fn build_donation_block(conf: &web::Data) -> String { } .into_string() } else { - "".to_string() + String::new() } }