44 lines
1.0 KiB
Rust
44 lines
1.0 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use std::{
|
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
|
path::Path,
|
|
};
|
|
|
|
const CONFIG_FILE: &'static str = "config.json";
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
pub struct Config {
|
|
pub admin_passwords: Vec<String>,
|
|
pub cookies_https_only: bool,
|
|
pub cookies_domain: Option<String>,
|
|
pub listen: SocketAddr,
|
|
}
|
|
|
|
impl Default for Config {
|
|
fn default() -> Self {
|
|
Self {
|
|
admin_passwords: vec![],
|
|
cookies_https_only: false,
|
|
cookies_domain: None,
|
|
listen: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 10353),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn read_config(dir: &Path) -> Config {
|
|
let path = dir.join(CONFIG_FILE);
|
|
|
|
if !path.is_file() {
|
|
let config = Config::default();
|
|
std::fs::write(path, serde_json::to_string_pretty(&config).unwrap())
|
|
.expect("Cannot write config file");
|
|
config
|
|
} else {
|
|
serde_json::from_str(
|
|
std::str::from_utf8(&std::fs::read(path).expect("Cannot read config file"))
|
|
.expect("Bad encoding in config file"),
|
|
)
|
|
.expect("Bad JSON in config file")
|
|
}
|
|
}
|