fabaccess-bffh/src/config.rs

106 lines
3.3 KiB
Rust
Raw Normal View History

2020-12-12 13:58:04 +01:00
use std::default::Default;
2021-09-19 19:47:29 +02:00
use std::path::{Path, PathBuf};
2020-09-15 14:31:10 +02:00
use std::collections::HashMap;
2020-02-18 16:55:19 +01:00
2020-12-12 13:58:04 +01:00
use serde::{Serialize, Deserialize};
2020-09-15 14:31:10 +02:00
2020-12-12 13:58:04 +01:00
use crate::error::Result;
use crate::machine::MachineDescription;
use crate::db::machine::MachineIdentifier;
2020-12-14 12:39:01 +01:00
use crate::db::access::*;
2020-09-15 14:31:10 +02:00
2020-12-12 13:58:04 +01:00
pub fn read(path: &Path) -> Result<Config> {
2020-12-14 11:02:46 +01:00
serde_dhall::from_file(path)
.parse()
.map_err(Into::into)
2020-02-14 12:20:17 +01:00
}
#[derive(Debug, Clone, Serialize, Deserialize)]
2020-12-12 13:58:04 +01:00
pub struct Config {
/// A list of address/port pairs to listen on.
// TODO: This should really be a variant type; that is something that can figure out itself if
// it contains enough information to open a socket (i.e. it checks if it's a valid path (=>
// Unix socket) or IPv4/v6 address)
2020-09-15 14:31:10 +02:00
pub listens: Box<[Listen]>,
2020-02-14 12:20:17 +01:00
2020-12-12 13:58:04 +01:00
/// Machine descriptions to load
pub machines: HashMap<MachineIdentifier, MachineDescription>,
2020-12-14 11:02:46 +01:00
/// Actors to load and their configuration options
pub actors: HashMap<String, ModuleConfig>,
/// Initiators to load and their configuration options
pub initiators: HashMap<String, ModuleConfig>,
pub mqtt_url: String,
2020-12-14 12:39:01 +01:00
pub actor_connections: Box<[(String, String)]>,
pub init_connections: Box<[(String, String)]>,
2021-09-19 19:47:29 +02:00
pub db_path: PathBuf,
2020-02-14 12:20:17 +01:00
}
2020-02-18 16:55:19 +01:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Listen {
pub address: String,
pub port: Option<u16>,
}
2020-12-14 11:02:46 +01:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModuleConfig {
2020-12-14 12:39:01 +01:00
pub module: String,
2020-12-14 11:02:46 +01:00
pub params: HashMap<String, String>
}
impl Default for Config {
2020-02-18 16:55:19 +01:00
fn default() -> Self {
2020-12-14 11:02:46 +01:00
let mut actors: HashMap::<String, ModuleConfig> = HashMap::new();
let mut initiators: HashMap::<String, ModuleConfig> = HashMap::new();
2020-12-14 12:39:01 +01:00
let mut machines = HashMap::new();
2020-12-14 11:02:46 +01:00
actors.insert("Actor".to_string(), ModuleConfig {
2020-12-14 12:39:01 +01:00
module: "Shelly".to_string(),
2020-12-14 11:02:46 +01:00
params: HashMap::new(),
});
initiators.insert("Initiator".to_string(), ModuleConfig {
2020-12-14 12:39:01 +01:00
module: "TCP-Listen".to_string(),
2020-12-14 11:02:46 +01:00
params: HashMap::new(),
});
2020-12-14 12:39:01 +01:00
machines.insert("Testmachine".to_string(), MachineDescription {
name: "Testmachine".to_string(),
description: Some("A test machine".to_string()),
privs: PrivilegesBuf {
disclose: PermissionBuf::from_string("lab.test.read".to_string()),
read: PermissionBuf::from_string("lab.test.read".to_string()),
write: PermissionBuf::from_string("lab.test.write".to_string()),
manage: PermissionBuf::from_string("lab.test.admin".to_string()),
},
});
2020-12-12 13:58:04 +01:00
Config {
2020-12-14 11:02:46 +01:00
listens: Box::new([
Listen {
address: "localhost".to_string(),
port: Some(DEFAULT_PORT),
}
]),
2021-09-19 19:47:29 +02:00
machines,
actors,
initiators,
2020-12-14 11:02:46 +01:00
mqtt_url: "tcp://localhost:1883".to_string(),
2020-12-14 12:39:01 +01:00
actor_connections: Box::new([
("Testmachine".to_string(), "Actor".to_string()),
]),
init_connections: Box::new([
("Initiator".to_string(), "Testmachine".to_string()),
]),
2021-09-19 19:47:29 +02:00
db_path: PathBuf::from("/run/bffh/database"),
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;