HomeDisk/src/config.rs

56 lines
1.1 KiB
Rust
Raw Normal View History

2022-09-14 17:51:12 +00:00
use std::fs;
2022-09-20 18:02:45 +00:00
use serde::Deserialize;
2022-09-14 17:51:12 +00:00
2022-09-20 18:02:45 +00:00
#[derive(Debug, Clone, Deserialize)]
2022-09-14 17:51:12 +00:00
pub struct Config {
pub http: ConfigHTTP,
pub jwt: ConfigJWT,
pub storage: ConfigStorage,
}
2022-09-20 18:02:45 +00:00
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
2022-09-14 17:51:12 +00:00
pub struct ConfigHTTP {
pub host: String,
pub http_port: u16,
pub enable_https: bool,
pub https_port: u16,
2022-09-14 17:51:12 +00:00
pub cors: Vec<String>,
pub tls_cert: String,
pub tls_key: String,
2022-09-14 17:51:12 +00:00
}
2022-09-20 18:02:45 +00:00
#[derive(Debug, Clone, Deserialize)]
2022-09-14 17:51:12 +00:00
pub struct ConfigJWT {
pub secret: String,
pub expires: i64,
}
2022-09-20 18:02:45 +00:00
#[derive(Debug, Clone, Deserialize)]
2022-09-14 17:51:12 +00:00
pub struct ConfigStorage {
pub path: String,
}
impl Config {
/// Parse configuration file.
pub fn parse(path: &str) -> anyhow::Result<Self> {
let config_str = fs::read_to_string(path)?;
let config = toml::from_str(&config_str)?;
Ok(config)
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Test default configuration file.
#[test]
fn test_config() {
Config::parse("./config.toml").expect("Failed to parse configuration file");
}
}