mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-10-30 03:05:51 +01:00
Improve Settings
This commit is contained in:
parent
291e0070c4
commit
de617dbbf2
3 changed files with 33 additions and 22 deletions
|
@ -1,3 +1,5 @@
|
|||
use chrono::Utc;
|
||||
|
||||
use crate::errors::Error;
|
||||
use crate::management::{delete_key, Action, Pending};
|
||||
use crate::pending_path;
|
||||
|
@ -21,28 +23,35 @@ pub fn confirm_action(token: &str) -> Result<(), Error> {
|
|||
Ok(key) => key,
|
||||
Err(_) => return Err(Error::DeserializeData),
|
||||
};
|
||||
match key.action() {
|
||||
Action::Add => {
|
||||
let cert = parse_pem(key.data())?;
|
||||
let domain = match get_email_from_cert(&cert)?.split('@').last() {
|
||||
Some(domain) => domain.to_string(),
|
||||
None => return Err(Error::ParseEmail),
|
||||
};
|
||||
match sequoia_net::wkd::insert(
|
||||
&SETTINGS.folder_structure.root_folder,
|
||||
domain,
|
||||
SETTINGS.variant,
|
||||
&cert,
|
||||
) {
|
||||
Ok(_) => (),
|
||||
Err(_) => return Err(Error::AddingKey),
|
||||
}
|
||||
if Utc::now().timestamp() - key.timestamp() > SETTINGS.max_age {
|
||||
match fs::remove_file(pending_path) {
|
||||
Ok(_) => Err(Error::MissingPending),
|
||||
Err(_) => Err(Error::Inaccessible),
|
||||
}
|
||||
} else {
|
||||
match key.action() {
|
||||
Action::Add => {
|
||||
let cert = parse_pem(key.data())?;
|
||||
let domain = match get_email_from_cert(&cert)?.split('@').last() {
|
||||
Some(domain) => domain.to_string(),
|
||||
None => return Err(Error::ParseEmail),
|
||||
};
|
||||
match sequoia_net::wkd::insert(
|
||||
&SETTINGS.folder_structure.root_folder,
|
||||
domain,
|
||||
SETTINGS.variant,
|
||||
&cert,
|
||||
) {
|
||||
Ok(_) => (),
|
||||
Err(_) => return Err(Error::AddingKey),
|
||||
}
|
||||
}
|
||||
Action::Delete => delete_key(key.data())?,
|
||||
}
|
||||
match fs::remove_file(&pending_path) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(Error::Inaccessible),
|
||||
}
|
||||
Action::Delete => delete_key(key.data())?,
|
||||
}
|
||||
match fs::remove_file(&pending_path) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(_) => Err(Error::Inaccessible),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ struct Email {
|
|||
async fn main() -> std::io::Result<()> {
|
||||
fs::create_dir_all(pending_path!())?;
|
||||
task::spawn(async {
|
||||
let mut metronome = time::interval(time::Duration::from_secs(60 * 60 * 3));
|
||||
let mut metronome = time::interval(time::Duration::from_secs(SETTINGS.cleanup_interval));
|
||||
loop {
|
||||
metronome.tick().await;
|
||||
clean_stale(SETTINGS.max_age).unwrap();
|
||||
|
|
|
@ -9,6 +9,7 @@ pub struct Settings {
|
|||
#[serde(with = "VariantDef")]
|
||||
pub variant: Variant,
|
||||
pub max_age: i64,
|
||||
pub cleanup_interval: u64,
|
||||
pub port: u16,
|
||||
pub folder_structure: FolderStructure,
|
||||
pub smtp_settings: MailSettings,
|
||||
|
@ -38,6 +39,7 @@ pub enum VariantDef {
|
|||
}
|
||||
|
||||
fn get_settings() -> Settings {
|
||||
println!("Reaing settings...");
|
||||
let content = fs::read_to_string("wkd.toml").unwrap();
|
||||
toml::from_str(&content).unwrap()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue