2020-05-20 19:05:59 +02:00
|
|
|
use std::error::Error;
|
|
|
|
|
|
|
|
use yaml_rust::{Yaml, YamlLoader};
|
|
|
|
|
|
|
|
/// Server configuration
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DatabaseConfig {
|
|
|
|
pub host: String,
|
|
|
|
pub name: String,
|
|
|
|
pub username: String,
|
|
|
|
pub password: String,
|
2020-07-08 13:33:55 +02:00
|
|
|
pub log_all_queries: bool,
|
2020-05-20 19:05:59 +02:00
|
|
|
}
|
|
|
|
|
2021-02-06 16:59:27 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RtcRelayConfig {
|
|
|
|
pub ip: String,
|
|
|
|
pub token: String,
|
|
|
|
pub ice_servers: Vec<String>,
|
|
|
|
pub max_users_per_calls: u64,
|
|
|
|
pub allow_video: bool,
|
|
|
|
pub max_users_per_video_calls: u64,
|
|
|
|
}
|
|
|
|
|
2021-04-11 17:08:30 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct IndependentPushService {
|
|
|
|
pub control_url: String,
|
|
|
|
pub control_token: String,
|
|
|
|
pub public_url: String,
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:05:59 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Config {
|
|
|
|
pub port: i32,
|
|
|
|
pub listen_address: String,
|
|
|
|
pub storage_url: String,
|
|
|
|
pub storage_path: String,
|
2021-04-15 11:41:33 +02:00
|
|
|
pub serve_storage_file: bool,
|
2021-02-20 09:31:38 +01:00
|
|
|
pub terms_url: String,
|
2021-02-20 11:21:45 +01:00
|
|
|
pub privacy_policy_url: String,
|
2021-02-20 09:44:34 +01:00
|
|
|
pub play_store_url: String,
|
|
|
|
pub android_direct_download_url: String,
|
2020-05-20 19:05:59 +02:00
|
|
|
pub proxy: Option<String>,
|
2021-02-06 16:38:51 +01:00
|
|
|
pub verbose_mode: bool,
|
2021-04-11 17:08:30 +02:00
|
|
|
pub independent_push_service: Option<IndependentPushService>,
|
2021-02-06 16:59:27 +01:00
|
|
|
pub database: DatabaseConfig,
|
|
|
|
pub rtc_relay: Option<RtcRelayConfig>,
|
2020-05-20 19:05:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Globally available configuration
|
|
|
|
static mut CONF: Option<Config> = None;
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
fn yaml_i32(parsed: &Yaml, name: &str) -> i32 {
|
|
|
|
parsed[name].as_i64().expect(format!("{} is missing (int)", name).as_str()) as i32
|
|
|
|
}
|
|
|
|
|
2021-02-06 16:59:27 +01:00
|
|
|
fn yaml_u64(parsed: &Yaml, name: &str) -> u64 {
|
|
|
|
parsed[name].as_i64().expect(format!("{} is missing (int)", name).as_str()) as u64
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:05:59 +02:00
|
|
|
fn yaml_str(parsed: &Yaml, name: &str) -> String {
|
|
|
|
parsed[name].as_str().expect(format!("{} is missing (str)", name).as_str()).to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn yaml_bool(parsed: &Yaml, name: &str) -> bool {
|
|
|
|
parsed[name].as_bool().expect(format!("{} is missing (bool)", name).as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Load the configuration from a given path
|
|
|
|
pub fn load(path: &str) -> Result<(), Box<dyn Error>> {
|
|
|
|
|
|
|
|
// Read & parse configuration
|
|
|
|
let conf_str = std::fs::read_to_string(path)?;
|
|
|
|
let parsed = YamlLoader::load_from_str(&conf_str)?;
|
|
|
|
|
|
|
|
if parsed.len() != 1 {
|
|
|
|
panic!("parsed.len() != 1");
|
|
|
|
}
|
|
|
|
let parsed = &parsed[0];
|
|
|
|
|
|
|
|
// Read configuration
|
|
|
|
let parsed_db = &parsed["database"];
|
|
|
|
let database_conf = DatabaseConfig {
|
|
|
|
host: Config::yaml_str(parsed_db, "host"),
|
|
|
|
name: Config::yaml_str(parsed_db, "name"),
|
|
|
|
username: Config::yaml_str(parsed_db, "username"),
|
|
|
|
password: Config::yaml_str(parsed_db, "password"),
|
2021-02-06 16:59:27 +01:00
|
|
|
log_all_queries: Config::yaml_bool(parsed_db, "log-all-queries"),
|
|
|
|
};
|
|
|
|
|
|
|
|
let parsed_rtc = &parsed["rtc-relay"];
|
|
|
|
let rtc_config = match parsed_rtc.is_badvalue() {
|
|
|
|
true => None,
|
|
|
|
false => Some(RtcRelayConfig {
|
|
|
|
ip: Config::yaml_str(parsed_rtc, "ip"),
|
|
|
|
token: Config::yaml_str(parsed_rtc, "token"),
|
|
|
|
ice_servers: parsed_rtc["ice-servers"].as_vec().unwrap().iter().map(|f| f.as_str().unwrap().to_string()).collect(),
|
|
|
|
max_users_per_calls: Config::yaml_u64(parsed_rtc, "max-users-per-calls"),
|
|
|
|
allow_video: Config::yaml_bool(parsed_rtc, "allow-video"),
|
|
|
|
max_users_per_video_calls: Config::yaml_u64(parsed_rtc, "max-users-per-video-calls"),
|
|
|
|
})
|
2020-05-20 19:05:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let proxy = Config::yaml_str(parsed, "proxy");
|
|
|
|
|
2021-04-11 17:08:30 +02:00
|
|
|
let parsed_independent_push_service = &parsed["independent-push-service"];
|
|
|
|
let independent_push_service = match parsed_independent_push_service.is_badvalue() {
|
|
|
|
true => None,
|
|
|
|
false => Some(IndependentPushService {
|
|
|
|
control_url: Config::yaml_str(parsed_independent_push_service, "control-url"),
|
|
|
|
control_token: Config::yaml_str(parsed_independent_push_service, "control-token"),
|
|
|
|
public_url: Config::yaml_str(parsed_independent_push_service, "public-url"),
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2020-05-20 19:05:59 +02:00
|
|
|
let conf = Config {
|
|
|
|
port: Config::yaml_i32(parsed, "server-port") as i32,
|
|
|
|
listen_address: Config::yaml_str(parsed, "server-address"),
|
|
|
|
|
|
|
|
storage_url: Config::yaml_str(parsed, "storage-url"),
|
|
|
|
storage_path: Config::yaml_str(parsed, "storage-path"),
|
|
|
|
|
2021-04-15 11:41:33 +02:00
|
|
|
serve_storage_file: Config::yaml_bool(parsed, "serve-storage-files"),
|
|
|
|
|
2021-02-20 09:31:38 +01:00
|
|
|
terms_url: Config::yaml_str(parsed, "terms-url"),
|
2021-02-20 11:21:45 +01:00
|
|
|
privacy_policy_url: Config::yaml_str(parsed, "privacy-policy-url"),
|
2021-02-20 09:31:38 +01:00
|
|
|
|
2021-02-20 09:44:34 +01:00
|
|
|
play_store_url: Config::yaml_str(parsed, "play-store-url"),
|
|
|
|
android_direct_download_url: Config::yaml_str(parsed, "android-direct-download-url"),
|
|
|
|
|
2020-05-20 19:05:59 +02:00
|
|
|
proxy: match proxy.as_ref() {
|
|
|
|
"none" => None,
|
|
|
|
s => Some(s.to_string())
|
|
|
|
},
|
|
|
|
|
2021-02-06 16:38:51 +01:00
|
|
|
verbose_mode: Config::yaml_bool(parsed, "verbose-mode"),
|
2021-02-06 16:59:27 +01:00
|
|
|
|
2021-04-11 17:08:30 +02:00
|
|
|
independent_push_service,
|
|
|
|
|
2021-02-06 16:59:27 +01:00
|
|
|
database: database_conf,
|
|
|
|
|
|
|
|
rtc_relay: rtc_config,
|
2020-05-20 19:05:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Save new configuration in memory
|
|
|
|
unsafe {
|
|
|
|
CONF = Some(conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-05-21 15:28:07 +02:00
|
|
|
|
|
|
|
/// Get the address this server listen to
|
|
|
|
pub fn server_listen_address(&self) -> String {
|
|
|
|
format!("{}:{}", self.listen_address, self.port)
|
|
|
|
}
|
2021-04-12 17:00:08 +02:00
|
|
|
|
|
|
|
/// Check if independent push notifications service is enabled
|
|
|
|
pub fn is_independent_push_notifications_service_enabled(&self) -> bool {
|
|
|
|
self.independent_push_service.is_some()
|
|
|
|
}
|
2020-05-20 19:05:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an instance of the configuration
|
|
|
|
pub fn conf() -> &'static Config {
|
|
|
|
unsafe {
|
|
|
|
return &CONF.as_ref().unwrap();
|
|
|
|
}
|
|
|
|
}
|