Process config before using it

This commit is contained in:
Joscha 2023-08-07 15:00:13 +02:00
parent 4f69f5cb21
commit 26ee2ff019
3 changed files with 40 additions and 19 deletions

View file

@ -54,24 +54,14 @@ impl Default for Web {
} }
} }
impl Web {
pub fn base(&self) -> String {
self.base
.strip_suffix('/')
.unwrap_or(&self.base)
.to_string()
}
}
#[derive(Debug, Default, Deserialize)] #[derive(Debug, Default, Deserialize)]
pub struct Config { struct ConfigFile {
pub repo: Repo, repo: Repo,
pub web: Web, web: Web,
} }
impl Config { impl ConfigFile {
pub fn load(path: &Path) -> somehow::Result<Self> { fn load(path: &Path) -> somehow::Result<Self> {
info!(path = %path.display(), "Loading config");
let config = match fs::read_to_string(path) { let config = match fs::read_to_string(path) {
Ok(str) => toml::from_str(&str)?, Ok(str) => toml::from_str(&str)?,
Err(e) if e.kind() == ErrorKind::NotFound => { Err(e) if e.kind() == ErrorKind::NotFound => {
@ -81,7 +71,38 @@ impl Config {
Err(e) => Err(e)?, Err(e) => Err(e)?,
}; };
debug!("Loaded config:\n{config:#?}");
Ok(config) Ok(config)
} }
fn web_base(&self) -> String {
self.web
.base
.strip_prefix('/')
.unwrap_or(&self.web.base)
.strip_suffix('/')
.unwrap_or(&self.web.base)
.to_string()
}
}
pub struct Config {
pub repo_name: String,
pub repo_update_delay: Duration,
pub web_base: String,
}
impl Config {
pub fn load(path: &Path) -> somehow::Result<Self> {
info!(path = %path.display(), "Loading config");
let config_file = ConfigFile::load(path)?;
debug!("Loaded config file:\n{config_file:#?}");
let web_base = config_file.web_base();
Ok(Self {
repo_name: config_file.repo.name,
repo_update_delay: config_file.repo.update_delay,
web_base,
})
}
} }

View file

@ -31,6 +31,6 @@ async fn recurring_task(state: &Server) {
pub async fn run(state: Server) { pub async fn run(state: Server) {
loop { loop {
recurring_task(&state).await; recurring_task(&state).await;
tokio::time::sleep(state.config.repo.update_delay).await; tokio::time::sleep(state.config.repo_update_delay).await;
} }
} }

View file

@ -32,8 +32,8 @@ impl Base {
Tab::Queue => "queue", Tab::Queue => "queue",
}; };
Self { Self {
root: config.web.base(), root: config.web_base.clone(),
repo_name: config.repo.name.clone(), repo_name: config.repo_name.clone(),
current: current.to_string(), current: current.to_string(),
} }
} }