bfile/janitor/src/env.rs

43 lines
1.3 KiB
Rust
Raw Normal View History

2023-10-11 16:06:07 +02:00
2023-10-12 12:39:28 +02:00
use std::{error::Error, env::var, time::Duration, path::Path};
2023-10-11 16:06:07 +02:00
#[derive(Debug, Clone)]
pub struct RedisEnv {
pub pass: String,
pub host: String,
pub port: u16,
pub prefix: String
}
#[derive(Debug, Clone)]
pub struct Env {
pub redis: RedisEnv,
pub clean_del: Duration,
2023-10-12 12:39:28 +02:00
pub clean_errdel: Duration,
pub usercont_dir: String
2023-10-11 16:06:07 +02:00
}
impl Env {
pub fn load() -> Result<Env, Box<dyn Error>> {
Ok(
Env {
redis: RedisEnv {
pass: var("REDIS_PASS")?.to_string(),
host: var("REDIS_HOST")?.to_string(),
port: var("REDIS_PORT")?.parse()?,
2023-10-12 12:39:28 +02:00
prefix: var("REDIS_PREFIX")?.to_string()
2023-10-11 16:06:07 +02:00
},
clean_del: parse_duration::parse(var("CLEAN_DEL")?.as_str())?,
2023-10-12 12:39:28 +02:00
clean_errdel: parse_duration::parse(var("CLEAN_ERRDEL")?.as_str())?,
usercont_dir: {
let dir = var("USERCONTENT_DIR")?;
let dir = dir.as_str();
if ! Path::new(dir).is_dir() {
return Err("Path specified in USERCONTENT_DIR is not a directory!".into());
}
dir.to_string()
}
2023-10-11 16:06:07 +02:00
}
)
}
}