70 lines
2.1 KiB
Rust
70 lines
2.1 KiB
Rust
use dirs;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct AtomAuthor {
|
|
pub name: String,
|
|
pub website: String,
|
|
pub email: String,
|
|
}
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct AtomConfig {
|
|
pub author: AtomAuthor,
|
|
pub feed_name: String,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
pub struct ServerConfig {
|
|
pub server_root: PathBuf,
|
|
pub listen_port: u16,
|
|
pub bind_address: [u8; 4],
|
|
pub server_domain: String,
|
|
|
|
pub atom_config: AtomConfig,
|
|
}
|
|
|
|
impl ServerConfig {
|
|
fn read_config() -> Option<String> {
|
|
let _test = dirs::config_dir();
|
|
let config_paths: Vec<Option<PathBuf>> =
|
|
vec![dirs::config_dir(), Some(PathBuf::from("/etc"))];
|
|
let valid_conf = config_paths
|
|
.iter()
|
|
.find_map(|p| {
|
|
p.as_ref().and_then(|path| {
|
|
let config_path = path.join("mdws/config.toml");
|
|
if config_path.exists() {
|
|
Some(config_path)
|
|
} else {
|
|
None
|
|
}
|
|
})
|
|
})
|
|
.and_then(|p| if p.is_file() { Some(p) } else { None });
|
|
match valid_conf {
|
|
Some(config_file) => std::fs::read_to_string(config_file).ok(),
|
|
None => None,
|
|
}
|
|
}
|
|
|
|
pub fn get_config() -> ServerConfig {
|
|
match Self::read_config().and_then(|t| toml::from_str(t.as_str()).ok()) {
|
|
Some(toml) => toml,
|
|
None => ServerConfig {
|
|
server_root: PathBuf::from("/srv/mdws/root"),
|
|
listen_port: 3030,
|
|
bind_address: [127, 0, 0, 1],
|
|
server_domain: "127.0.0.1:3030".to_string(),
|
|
atom_config: AtomConfig {
|
|
author: AtomAuthor {
|
|
name: "NAME".to_string(),
|
|
website: "WEBSITE".to_string(),
|
|
email: "EMAIL".to_string(),
|
|
},
|
|
feed_name: "FEED".to_string(),
|
|
},
|
|
},
|
|
}
|
|
}
|
|
}
|