refactor
This commit is contained in:
parent
37e69a851c
commit
cef45e43a6
6 changed files with 79 additions and 86 deletions
|
@ -13,7 +13,7 @@ pub struct Config {
|
|||
}
|
||||
|
||||
fn read_json_file(f: &str) -> Option<Value> {
|
||||
return serde_json::from_str(&std::fs::read_to_string(f).ok()?).ok()?;
|
||||
serde_json::from_str(&std::fs::read_to_string(f).ok()?).ok()?
|
||||
}
|
||||
|
||||
impl Config {
|
||||
|
@ -24,23 +24,23 @@ impl Config {
|
|||
}
|
||||
|
||||
pub fn name(&self) -> Option<String> {
|
||||
return Option::from(self.root.get("name")?.as_str()?.to_string());
|
||||
Option::from(self.root.get("name")?.as_str()?.to_string())
|
||||
}
|
||||
|
||||
pub fn email(&self) -> Option<String> {
|
||||
return Option::from(self.root.get("email")?.as_str()?.to_string());
|
||||
Option::from(self.root.get("email")?.as_str()?.to_string())
|
||||
}
|
||||
|
||||
pub fn xmr_address(&self) -> Option<String> {
|
||||
return Option::from(self.root.get("xmr_address")?.as_str()?.to_string());
|
||||
Option::from(self.root.get("xmr_address")?.as_str()?.to_string())
|
||||
}
|
||||
|
||||
fn color_n_fg(&self) -> Option<i64> {
|
||||
return Some(self.root.get("colors")?.get("fg")?.as_i64()?);
|
||||
self.root.get("colors")?.get("fg")?.as_i64()
|
||||
}
|
||||
|
||||
fn color_n_bg(&self) -> Option<i64> {
|
||||
return Some(self.root.get("colors")?.get("bg")?.as_i64()?);
|
||||
self.root.get("colors")?.get("bg")?.as_i64()
|
||||
}
|
||||
|
||||
pub fn fg_color(&self) -> Option<String> {
|
||||
|
@ -48,12 +48,12 @@ impl Config {
|
|||
let fg = col.get("special")?.get("foreground")?.as_str()?;
|
||||
if let Some(fg_n) = self.color_n_fg() {
|
||||
let n = fg_n - 1;
|
||||
let fg = col.get("colors")?.get(format!("color{}", n))?.as_str()?;
|
||||
let fg = col.get("colors")?.get(format!("color{n}"))?.as_str()?;
|
||||
return Some(fg.to_string());
|
||||
}
|
||||
return Some(fg.to_string());
|
||||
Some(fg.to_string())
|
||||
} else {
|
||||
return None;
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -62,12 +62,12 @@ impl Config {
|
|||
let fg = col.get("special")?.get("background")?.as_str()?;
|
||||
if let Some(bg_n) = self.color_n_bg() {
|
||||
let n = bg_n - 1;
|
||||
let fg = col.get("colors")?.get(format!("color{}", n))?.as_str()?;
|
||||
let fg = col.get("colors")?.get(format!("color{n}"))?.as_str()?;
|
||||
return Some(fg.to_string());
|
||||
}
|
||||
return Some(fg.to_string());
|
||||
Some(fg.to_string())
|
||||
} else {
|
||||
return None;
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -75,6 +75,6 @@ impl Config {
|
|||
let settings = self.root.get("notify")?.get("gotify")?;
|
||||
let host = settings.get("host")?.as_str()?.to_string();
|
||||
let token = settings.get("token")?.as_str()?.to_string();
|
||||
return Some(GotifySettings { host, token });
|
||||
Some(GotifySettings { host, token })
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,9 +32,7 @@ async fn main() -> std::io::Result<()> {
|
|||
.service(pages::index::message_page)
|
||||
.service(pages::index::message_post);
|
||||
|
||||
let app = web_base::assign_pages(csrf.clone(), app);
|
||||
|
||||
app
|
||||
web_base::assign_pages(csrf.clone(), app)
|
||||
})
|
||||
.bind(("0.0.0.0", 8080))?
|
||||
.run()
|
||||
|
|
|
@ -5,8 +5,7 @@ pub fn encrypt(msg: String) -> String {
|
|||
let pub_key = pgp
|
||||
.import_key(&std::fs::read_to_string("./config/pub.key").unwrap())
|
||||
.unwrap();
|
||||
let c = pgp.encrypt(&pub_key, &msg).unwrap();
|
||||
return c;
|
||||
pgp.encrypt(&pub_key, &msg).unwrap()
|
||||
}
|
||||
|
||||
pub fn save_msg(msg: String, name: &str) {
|
||||
|
|
|
@ -10,7 +10,7 @@ pub(crate) async fn build_site(
|
|||
shadow: bool,
|
||||
config: &Data<Config>,
|
||||
) -> HttpResponse<String> {
|
||||
let BOOTSTRAP = html! {
|
||||
let bootstrap = html! {
|
||||
link href="/bootstrap.min.css" rel="stylesheet";
|
||||
link href="/bootstrap-icons.css" rel="stylesheet";
|
||||
link href="/bootstrap.bundle.min.js" rel="stylesheet";
|
||||
|
@ -40,7 +40,7 @@ pub(crate) async fn build_site(
|
|||
title {
|
||||
(title)
|
||||
};
|
||||
(BOOTSTRAP)
|
||||
(bootstrap)
|
||||
};
|
||||
body style=(c_style) class=(c_class) {
|
||||
style { (g_style) };
|
||||
|
@ -49,5 +49,5 @@ pub(crate) async fn build_site(
|
|||
};
|
||||
};
|
||||
|
||||
return HttpResponse::Ok().message_body(r.into_string()).unwrap();
|
||||
HttpResponse::Ok().message_body(r.into_string()).unwrap()
|
||||
}
|
||||
|
|
|
@ -16,18 +16,17 @@ pub async fn message_post(r: HttpRequest, f: Form<MessageForm>) -> impl Responde
|
|||
let config: &web::Data<config::Config> = r.app_data().unwrap();
|
||||
crate::msg::save_msg(f.message.clone(), &f.msg_name.to_string());
|
||||
crate::notification::notify(
|
||||
&format!("New Message from {}", f.msg_name.to_string()),
|
||||
&format!("New Message from {}", f.msg_name),
|
||||
"New Message",
|
||||
config.clone(),
|
||||
)
|
||||
.await;
|
||||
return web_base::func::redirect("/message");
|
||||
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 = html! {
|
||||
div class="container" style="margin-top: 25px" {
|
||||
|
@ -42,7 +41,7 @@ pub async fn message_page(r: HttpRequest) -> impl Responder {
|
|||
}
|
||||
};
|
||||
|
||||
return pages::html_fn::build_site(resp.into_string(), "Message", false, true, config).await;
|
||||
pages::html_fn::build_site(resp.into_string(), "Message", false, true, config).await
|
||||
}
|
||||
|
||||
#[get("/mirrors.txt")]
|
||||
|
@ -61,13 +60,11 @@ pub async fn mirrors(r: HttpRequest) -> impl Responder {
|
|||
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;
|
||||
return HttpResponse::Ok().message_body(content).unwrap();
|
||||
}
|
||||
let res: HttpResponse<String> = HttpResponse::NotFound()
|
||||
HttpResponse::NotFound()
|
||||
.message_body("".to_string())
|
||||
.unwrap();
|
||||
return res;
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[get("/public_key")]
|
||||
|
@ -80,7 +77,7 @@ pub async fn public_key(r: HttpRequest) -> impl Responder {
|
|||
|
||||
let pgp = gnupg::GnuPG::new().unwrap();
|
||||
let key_name = pgp.import_key(&key).unwrap().name;
|
||||
let key = key.replace("\n", "<br>");
|
||||
let key = key.replace('\n', "<br>");
|
||||
|
||||
let resp = html! {
|
||||
div class="container" style="margin-top: 25px" {
|
||||
|
@ -100,23 +97,21 @@ pub async fn public_key(r: HttpRequest) -> impl Responder {
|
|||
}
|
||||
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()
|
||||
return HttpResponse::Ok()
|
||||
.insert_header(header::ContentType::plaintext())
|
||||
.message_body(key_data)
|
||||
.unwrap();
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
let res: HttpResponse<String> = HttpResponse::NotFound()
|
||||
HttpResponse::NotFound()
|
||||
.message_body("".to_string())
|
||||
.unwrap();
|
||||
return res;
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn build_information_block(conf: &web::Data<config::Config>) -> String {
|
||||
let name = conf.name().unwrap();
|
||||
return html! {
|
||||
html! {
|
||||
div class="container border-dark" style="margin-top: 20px" {
|
||||
img src="/assets/me" height="200" width="200" alt="Me" class="rounded";
|
||||
br;br;
|
||||
|
@ -124,7 +119,7 @@ fn build_information_block(conf: &web::Data<config::Config>) -> String {
|
|||
hr;
|
||||
}
|
||||
}
|
||||
.into_string();
|
||||
.into_string()
|
||||
}
|
||||
|
||||
fn build_contact_block(conf: &web::Data<config::Config>) -> String {
|
||||
|
@ -140,7 +135,7 @@ fn build_contact_block(conf: &web::Data<config::Config>) -> String {
|
|||
false => "".to_string(),
|
||||
};
|
||||
|
||||
return html! {
|
||||
html! {
|
||||
div class="container border-dark" {
|
||||
h1 {
|
||||
span class="bi bi-person-lines-fill" style="vertical-align: middle;";
|
||||
|
@ -152,9 +147,9 @@ fn build_contact_block(conf: &web::Data<config::Config>) -> String {
|
|||
hr;
|
||||
}
|
||||
}
|
||||
.into_string();
|
||||
.into_string()
|
||||
} else {
|
||||
return "".to_string();
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -175,7 +170,7 @@ fn build_donation_block(conf: &web::Data<config::Config>) -> String {
|
|||
}
|
||||
.into_string()
|
||||
} else {
|
||||
return "".to_string();
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -191,6 +186,5 @@ pub(crate) async fn index(conf: web::Data<config::Config>) -> impl Responder {
|
|||
{donation_block}
|
||||
"
|
||||
);
|
||||
let r = crate::pages::html_fn::build_site(content, "About Me", false, true, &conf).await;
|
||||
return r;
|
||||
crate::pages::html_fn::build_site(content, "About Me", false, true, &conf).await
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue