HomeDisk/types/src/config/types.rs

68 lines
1.7 KiB
Rust
Raw Normal View History

2022-06-08 19:16:12 +00:00
//! Configuration file types
2022-06-18 10:35:23 +00:00
use std::fs;
2022-04-19 19:10:36 +00:00
use serde::{Deserialize, Serialize};
2022-06-18 10:35:23 +00:00
use crate::option_return;
2022-06-08 19:16:12 +00:00
/// Config type
2022-04-19 19:10:36 +00:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
2022-06-18 10:35:23 +00:00
/// Configure HTTP settings
2022-04-19 19:10:36 +00:00
pub http: ConfigHTTP,
2022-06-18 10:35:23 +00:00
/// Configure Json Web Token settings
2022-04-19 19:10:36 +00:00
pub jwt: ConfigJWT,
2022-06-18 10:35:23 +00:00
/// Configure storage settings
2022-04-24 19:31:50 +00:00
pub storage: ConfigStorage,
2022-04-19 19:10:36 +00:00
}
2022-06-08 19:16:12 +00:00
/// HTTP config
2022-04-19 19:10:36 +00:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigHTTP {
2022-06-07 20:36:26 +00:00
/// HTTP Host
2022-04-19 19:10:36 +00:00
pub host: String,
2022-06-07 20:36:26 +00:00
/// Port HTTP Port
2022-04-19 19:10:36 +00:00
pub port: u16,
/// [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) Domains (e.g ["site1.example.com", "site2.example.com"])
2022-04-19 19:10:36 +00:00
pub cors: Vec<String>,
}
2022-06-08 19:16:12 +00:00
/// Json Web Token config
2022-04-19 19:10:36 +00:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigJWT {
2022-06-08 22:02:20 +00:00
/// JWT Secret string (used to sign tokens)
2022-04-19 19:10:36 +00:00
pub secret: String,
2022-06-08 22:02:20 +00:00
/// Token expiration time in hours
2022-04-23 10:52:56 +00:00
pub expires: i64,
2022-04-19 19:10:36 +00:00
}
2022-04-24 19:31:50 +00:00
2022-06-08 19:16:12 +00:00
/// Storage config
2022-04-24 19:31:50 +00:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigStorage {
2022-06-07 20:36:26 +00:00
/// Directory where user files will be stored
2022-04-24 19:31:50 +00:00
pub path: String,
}
2022-06-18 10:35:23 +00:00
#[cfg(feature = "config")]
impl Config {
/// Parse configuration file.
///
2022-06-18 10:35:23 +00:00
/// ```no_run
/// use homedisk_types::config::Config;
///
/// let config = Config::parse().unwrap();
/// ```
pub fn parse() -> anyhow::Result<Config> {
// get file path of config file
let config_dir = option_return!(dirs::config_dir(), "find config dir")?;
let config_path = format!("{}/homedisk/config.toml", config_dir.to_string_lossy());
// read file content to string
let config = fs::read_to_string(config_path)?;
// parse config and return it
Ok(toml::from_str(&config)?)
}
}