0
0
Fork 0
mirror of https://git.verdigado.com/NB-Public/simple-wkd.git synced 2024-12-06 14:52:41 +01:00

Add toml settings

This commit is contained in:
Delta1925 2023-04-14 00:52:54 +02:00
parent d3a1860a2d
commit 291e0070c4
No known key found for this signature in database
GPG key ID: 1C21ACE44193CB25
8 changed files with 121 additions and 18 deletions

View file

@ -1,8 +1,8 @@
use crate::errors::Error;
use crate::management::{delete_key, Action, Pending};
use crate::pending_path;
use crate::settings::SETTINGS;
use crate::utils::{get_email_from_cert, parse_pem};
use crate::PENDING;
use crate::{pending_path, PATH, VARIANT};
use std::fs;
use std::path::Path;
@ -28,7 +28,12 @@ pub fn confirm_action(token: &str) -> Result<(), Error> {
Some(domain) => domain.to_string(),
None => return Err(Error::ParseEmail),
};
match sequoia_net::wkd::insert(PATH, domain, VARIANT, &cert) {
match sequoia_net::wkd::insert(
&SETTINGS.folder_structure.root_folder,
domain,
SETTINGS.variant,
&cert,
) {
Ok(_) => (),
Err(_) => return Err(Error::AddingKey),
}

View file

@ -1,8 +1,10 @@
mod confirmation;
mod errors;
mod management;
mod settings;
mod utils;
use crate::settings::SETTINGS;
use crate::utils::key_exists;
use self::confirmation::{confirm_action, send_confirmation_email};
@ -10,18 +12,11 @@ use self::management::{clean_stale, store_pending_addition, store_pending_deleti
use self::utils::{gen_random_token, get_email_from_cert, parse_pem};
use actix_web::{get, post, web, App, HttpServer, Result};
use sequoia_net::wkd::Variant;
use serde::Deserialize;
use std::fs;
use std::path::Path;
use tokio::{task, time};
const PATH: &str = "data";
const PENDING: &str = "pending";
const MAX_AGE: i64 = 0;
const VARIANT: Variant = Variant::Advanced;
const PORT: u16 = 8080;
#[derive(Deserialize, Debug)]
struct Pem {
key: String,
@ -44,11 +39,11 @@ async fn main() -> std::io::Result<()> {
let mut metronome = time::interval(time::Duration::from_secs(60 * 60 * 3));
loop {
metronome.tick().await;
clean_stale(MAX_AGE).unwrap();
clean_stale(SETTINGS.max_age).unwrap();
}
});
HttpServer::new(|| App::new().service(submit).service(confirm).service(delete))
.bind(("127.0.0.1", PORT))?
.bind(("127.0.0.1", SETTINGS.port))?
.run()
.await
}

View file

@ -1,7 +1,7 @@
use crate::errors::Error;
use crate::pending_path;
use crate::settings::SETTINGS;
use crate::utils::get_user_file_path;
use crate::PENDING;
use crate::{pending_path, PATH};
use chrono::Utc;
use serde::{Deserialize, Serialize};
@ -95,7 +95,7 @@ pub fn clean_stale(max_age: i64) -> Result<(), Error> {
}
pub fn delete_key(email: &str) -> Result<(), Error> {
let path = Path::new(PATH).join(get_user_file_path(email)?);
let path = Path::new(&SETTINGS.folder_structure.root_folder).join(get_user_file_path(email)?);
match fs::remove_file(path) {
Ok(_) => Ok(()),
Err(_) => Err(Error::Inaccessible),

45
src/settings.rs Normal file
View file

@ -0,0 +1,45 @@
use std::fs;
use once_cell::sync::Lazy;
use sequoia_net::wkd::Variant;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Settings {
#[serde(with = "VariantDef")]
pub variant: Variant,
pub max_age: i64,
pub port: u16,
pub folder_structure: FolderStructure,
pub smtp_settings: MailSettings,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct FolderStructure {
pub root_folder: String,
pub pending_folder: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct MailSettings {
pub smtp_host: String,
pub smtp_username: String,
pub smtp_password: String,
pub smtp_port: u16,
pub mail_from: String,
pub mail_subject: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(remote = "Variant")]
pub enum VariantDef {
Advanced,
Direct,
}
fn get_settings() -> Settings {
let content = fs::read_to_string("wkd.toml").unwrap();
toml::from_str(&content).unwrap()
}
pub static SETTINGS: Lazy<Settings> = Lazy::new(get_settings);

View file

@ -1,5 +1,5 @@
use crate::errors::Error;
use crate::{PATH, PENDING, VARIANT};
use crate::settings::SETTINGS;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use sequoia_net::wkd::Url;
@ -9,7 +9,8 @@ use std::path::{Path, PathBuf};
#[macro_export]
macro_rules! pending_path {
() => {
Path::new(PATH).join(PENDING)
Path::new(&SETTINGS.folder_structure.root_folder)
.join(&SETTINGS.folder_structure.pending_folder)
};
}
@ -45,7 +46,7 @@ pub fn get_user_file_path(email: &str) -> Result<PathBuf, Error> {
Ok(wkd_url) => wkd_url,
Err(_) => return Err(Error::PathGeneration),
};
match wkd_url.to_file_path(VARIANT) {
match wkd_url.to_file_path(SETTINGS.variant) {
Ok(path) => Ok(path),
Err(_) => Err(Error::PathGeneration),
}