0
0
Fork 0
mirror of https://git.verdigado.com/NB-Public/simple-wkd.git synced 2024-10-30 07:05:51 +01:00

Refactor some code

This commit is contained in:
Delta1925 2023-04-18 18:18:16 +02:00
parent 88e7e6088d
commit f431246321
No known key found for this signature in database
GPG key ID: 1C21ACE44193CB25
4 changed files with 16 additions and 27 deletions

View file

@ -13,7 +13,7 @@ use std::fs;
use std::path::Path; use std::path::Path;
pub fn confirm_action(token: &str) -> Result<(Action, String)> { pub fn confirm_action(token: &str) -> Result<(Action, String)> {
let pending_path = pending_path!().join(token); let pending_path = pending_path().join(token);
let content = read_file(&pending_path)?; let content = read_file(&pending_path)?;
let key = toml::from_str::<Pending>(&content)?; let key = toml::from_str::<Pending>(&content)?;
if Utc::now().timestamp() - key.timestamp() > SETTINGS.max_age { if Utc::now().timestamp() - key.timestamp() > SETTINGS.max_age {

View file

@ -24,7 +24,7 @@ use std::env;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use tokio::{task, time}; use tokio::{task, time};
use utils::init_logger; use utils::{init_logger, pending_path, webpage_path};
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct Key { struct Key {
@ -49,7 +49,7 @@ async fn main() -> std::io::Result<()> {
if init_logger().is_err() { if init_logger().is_err() {
panic!("Could not set up logger!") panic!("Could not set up logger!")
}; };
fs::create_dir_all(pending_path!())?; fs::create_dir_all(pending_path())?;
task::spawn(async { task::spawn(async {
let mut metronome = time::interval(time::Duration::from_secs(SETTINGS.cleanup_interval)); let mut metronome = time::interval(time::Duration::from_secs(SETTINGS.cleanup_interval));
loop { loop {
@ -74,7 +74,7 @@ async fn main() -> std::io::Result<()> {
} }
async fn index(req: HttpRequest) -> Result<HttpResponse, CompatErr> { async fn index(req: HttpRequest) -> Result<HttpResponse, CompatErr> {
let path = webpage_path!().join(req.match_info().query("filename")); let path = webpage_path().join(req.match_info().query("filename"));
for file in &["", "index.html"] { for file in &["", "index.html"] {
let path = if file.is_empty() { let path = if file.is_empty() {
path.to_owned() path.to_owned()

View file

@ -1,6 +1,5 @@
use crate::pending_path;
use crate::settings::ROOT_FOLDER; use crate::settings::ROOT_FOLDER;
use crate::utils::{get_user_file_path, key_exists, read_file}; use crate::utils::{get_user_file_path, key_exists, read_file, pending_path};
use anyhow::Result; use anyhow::Result;
use chrono::Utc; use chrono::Utc;
@ -55,7 +54,7 @@ impl Pending {
fn store_pending(pending: &Pending, token: &str) -> Result<()> { fn store_pending(pending: &Pending, token: &str) -> Result<()> {
let serialized = toml::to_string(pending)?; let serialized = toml::to_string(pending)?;
fs::write(pending_path!().join(token), serialized)?; fs::write(pending_path().join(token), serialized)?;
Ok(()) Ok(())
} }
@ -73,7 +72,7 @@ pub fn store_pending_deletion(email: String, token: &str) -> Result<()> {
} }
pub fn clean_stale(max_age: i64) { pub fn clean_stale(max_age: i64) {
for path in fs::read_dir(pending_path!()).unwrap().flatten() { for path in fs::read_dir(pending_path()).unwrap().flatten() {
let file_path = path.path(); let file_path = path.path();
let content = match read_file(&file_path) { let content = match read_file(&file_path) {
Ok(content) => content, Ok(content) => content,

View file

@ -21,20 +21,6 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
#[macro_export]
macro_rules! pending_path {
() => {
Path::new(&ROOT_FOLDER).join("pending")
};
}
#[macro_export]
macro_rules! webpage_path {
() => {
Path::new("assets").join("webpage")
};
}
#[macro_export] #[macro_export]
macro_rules! validate_cert { macro_rules! validate_cert {
( $x:expr ) => { ( $x:expr ) => {
@ -45,6 +31,14 @@ macro_rules! validate_cert {
}; };
} }
pub fn pending_path() -> PathBuf {
Path::new(&ROOT_FOLDER).join("pending")
}
pub fn webpage_path() -> PathBuf {
Path::new("assets").join("webpage")
}
pub fn read_file(path: &PathBuf) -> Result<String> { pub fn read_file(path: &PathBuf) -> Result<String> {
if path.is_file() { if path.is_file() {
Ok(fs::read_to_string(path)?) Ok(fs::read_to_string(path)?)
@ -101,10 +95,6 @@ pub fn key_exists(email: &str) -> Result<bool> {
Ok(true) Ok(true)
} }
pub fn _get_filename(path: &Path) -> Option<&str> {
path.file_name()?.to_str()
}
pub fn custom_color_format( pub fn custom_color_format(
w: &mut dyn std::io::Write, w: &mut dyn std::io::Write,
now: &mut DeferredNow, now: &mut DeferredNow,
@ -150,7 +140,7 @@ pub fn init_logger() -> Result<LoggerHandle, FlexiLoggerError> {
} }
pub fn return_outcome(data: Result<&str, &CompatErr>) -> Result<HttpResponse> { pub fn return_outcome(data: Result<&str, &CompatErr>) -> Result<HttpResponse> {
let path = webpage_path!().join("status").join("index.html"); let path = webpage_path().join("status").join("index.html");
let template = read_file(&path)?; let template = read_file(&path)?;
let (page, message) = match data { let (page, message) = match data {
Ok(message) => (template.replace("((%s))", "Success!"), message.to_string()), Ok(message) => (template.replace("((%s))", "Success!"), message.to_string()),