2020-02-14 12:20:17 +01:00
|
|
|
use std::str::FromStr;
|
2020-02-18 16:55:19 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-02-16 16:02:03 +01:00
|
|
|
use serde::{Serialize, Deserialize};
|
2020-02-18 16:55:19 +01:00
|
|
|
use std::io::Read;
|
|
|
|
use std::fs::File;
|
2020-02-14 12:20:17 +01:00
|
|
|
|
|
|
|
use crate::error::Result;
|
|
|
|
|
2020-02-18 16:55:19 +01:00
|
|
|
use std::default::Default;
|
|
|
|
|
|
|
|
pub fn read(path: &Path) -> Result<Config> {
|
|
|
|
let mut fp = File::open(path)?;
|
|
|
|
let mut contents = String::new();
|
|
|
|
fp.read_to_string(&mut contents)?;
|
|
|
|
|
|
|
|
let config = toml::from_str(&contents)?;
|
|
|
|
Ok(config)
|
2020-02-14 12:20:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2020-02-14 12:20:17 +01:00
|
|
|
pub struct Config {
|
2020-09-10 10:39:46 +02:00
|
|
|
pub db: PathBuf,
|
2020-02-16 16:02:03 +01:00
|
|
|
pub machinedb: PathBuf,
|
|
|
|
pub passdb: PathBuf,
|
2020-02-18 16:55:19 +01:00
|
|
|
pub(crate) access: Access,
|
|
|
|
pub listen: Box<[Listen]>,
|
2020-09-15 11:38:15 +02:00
|
|
|
pub mqtt_url: String,
|
2020-02-14 12:20:17 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 16:02:03 +01:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2020-02-14 12:20:17 +01:00
|
|
|
pub struct Access {
|
|
|
|
pub(crate) model: PathBuf,
|
|
|
|
pub(crate) policy: PathBuf
|
|
|
|
}
|
2020-02-18 16:55:19 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct Listen {
|
|
|
|
pub address: String,
|
|
|
|
pub port: Option<u16>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Config {
|
2020-09-10 10:39:46 +02:00
|
|
|
db: PathBuf::from_str("/tmp/bffh.db").unwrap(),
|
2020-02-18 16:55:19 +01:00
|
|
|
machinedb: PathBuf::from_str("/tmp/machines.db").unwrap(),
|
|
|
|
access: Access {
|
|
|
|
model: PathBuf::from_str("/tmp/model.conf").unwrap(),
|
|
|
|
policy: PathBuf::from_str("/tmp/policy.csv").unwrap(),
|
|
|
|
},
|
|
|
|
passdb: PathBuf::from_str("/tmp/passwd.db").unwrap(),
|
|
|
|
listen: Box::new([Listen {
|
|
|
|
address: "127.0.0.1".to_string(),
|
|
|
|
port: Some(DEFAULT_PORT)
|
|
|
|
},
|
|
|
|
Listen {
|
|
|
|
address: "::1".to_string(),
|
|
|
|
port: Some(DEFAULT_PORT)
|
|
|
|
}]),
|
2020-09-15 11:38:15 +02:00
|
|
|
mqtt_url: "127.0.0.1:1883".to_string(),
|
2020-02-18 16:55:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The default port in the non-assignable i.e. free-use area
|
|
|
|
pub const DEFAULT_PORT: u16 = 59661;
|