add: cleanup routine to avoid stale handlers
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
use std::{fs, path::Path};
|
||||
use std::{
|
||||
fs,
|
||||
path::Path,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use chrono::Utc;
|
||||
use log::info;
|
||||
use rouille::Request;
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
use crate::Rewriter;
|
||||
|
||||
@@ -18,7 +22,12 @@ pub struct Server {
|
||||
}
|
||||
|
||||
impl Server {
|
||||
pub fn new(rewriter: Rewriter, workspace_dir: String, template_dir: String, config_dir: String) -> Self {
|
||||
pub fn new(
|
||||
rewriter: Rewriter,
|
||||
workspace_dir: String,
|
||||
template_dir: String,
|
||||
config_dir: String,
|
||||
) -> Self {
|
||||
Self {
|
||||
rewriter,
|
||||
workspace_dir,
|
||||
@@ -27,9 +36,17 @@ impl Server {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cleanup(&mut self) -> Result<()> {
|
||||
self.rewriter.cleanup();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn register(&mut self, _request: &Request, ip: &str) -> Result<()> {
|
||||
info!("Registering {} as a handler", ip);
|
||||
self.rewriter.add_replacement(ip.to_string());
|
||||
let cleanup_time = Instant::now()
|
||||
.checked_add(Duration::from_secs(60 * 5))
|
||||
.ok_or(anyhow!("failed to convert time"))?;
|
||||
self.rewriter.add_replacement(ip.to_string(), cleanup_time);
|
||||
self.generate_config()?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -49,11 +66,13 @@ impl Server {
|
||||
let path = Path::new(&self.workspace_dir).join(&now.format("%Y/%m/%d/%s").to_string());
|
||||
let path = path.as_os_str().to_str().unwrap();
|
||||
fs::create_dir_all(path).with_context(|| "creating directory")?;
|
||||
self.rewriter.rewrite_folder(&self.template_dir, path).with_context(|| "generating configs")?;
|
||||
self.rewriter
|
||||
.rewrite_folder(&self.template_dir, path)
|
||||
.with_context(|| "generating configs")?;
|
||||
// Finally, symlink it to the output folder; only support Linux for now
|
||||
let symlink = Path::new(&self.workspace_dir).join("symlink.tmp");
|
||||
std::os::unix::fs::symlink(path, &symlink).with_context(|| "creating symlink")?;
|
||||
fs::rename(symlink, &self.config_dir).with_context(|| "renaming symlink")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user