2018-10-20 23:57:58 +01:00
|
|
|
use std;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use toml;
|
|
|
|
pub use tracker::TrackerMode;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct UDPConfig {
|
|
|
|
bind_address: String,
|
2018-10-21 21:39:39 +01:00
|
|
|
announce_interval: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UDPConfig {
|
|
|
|
pub fn get_address(&self) -> &str {
|
|
|
|
self.bind_address.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_announce_interval(&self) -> u32 {
|
|
|
|
self.announce_interval
|
|
|
|
}
|
2018-10-20 23:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct HTTPConfig {
|
|
|
|
bind_address: String,
|
|
|
|
access_tokens: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
2018-10-21 21:39:39 +01:00
|
|
|
impl HTTPConfig {
|
|
|
|
pub fn get_address(&self) -> &str {
|
|
|
|
self.bind_address.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_access_tokens(&self) -> &HashMap<String, String> {
|
|
|
|
&self.access_tokens
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:57:58 +01:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct Configuration {
|
2018-10-21 21:39:39 +01:00
|
|
|
mode: TrackerMode,
|
2018-10-20 23:57:58 +01:00
|
|
|
udp: UDPConfig,
|
|
|
|
http: Option<HTTPConfig>,
|
2018-10-25 20:49:25 +01:00
|
|
|
log_level: Option<String>,
|
2018-12-06 23:58:31 +00:00
|
|
|
db_path: Option<String>,
|
2018-12-11 01:03:14 +00:00
|
|
|
cleanup_interval: Option<u64>,
|
2018-10-20 23:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ConfigError {
|
|
|
|
IOError(std::io::Error),
|
|
|
|
ParseError(toml::de::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for ConfigError {
|
|
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
ConfigError::IOError(e) => e.fmt(formatter),
|
|
|
|
ConfigError::ParseError(e) => e.fmt(formatter),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::error::Error for ConfigError {}
|
|
|
|
|
|
|
|
impl Configuration {
|
|
|
|
pub fn load(data: &[u8]) -> Result<Configuration, toml::de::Error> {
|
|
|
|
toml::from_slice(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn load_file(path: &str) -> Result<Configuration, ConfigError> {
|
|
|
|
match std::fs::read(path) {
|
|
|
|
Err(e) => Err(ConfigError::IOError(e)),
|
2018-12-06 23:58:31 +00:00
|
|
|
Ok(data) => match Self::load(data.as_slice()) {
|
|
|
|
Ok(cfg) => Ok(cfg),
|
|
|
|
Err(e) => Err(ConfigError::ParseError(e)),
|
|
|
|
},
|
2018-10-20 23:57:58 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-21 21:39:39 +01:00
|
|
|
|
|
|
|
pub fn get_mode(&self) -> &TrackerMode {
|
|
|
|
&self.mode
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_udp_config(&self) -> &UDPConfig {
|
|
|
|
&self.udp
|
|
|
|
}
|
|
|
|
|
2018-10-25 20:49:25 +01:00
|
|
|
pub fn get_log_level(&self) -> &Option<String> {
|
|
|
|
&self.log_level
|
|
|
|
}
|
|
|
|
|
2018-10-21 21:39:39 +01:00
|
|
|
pub fn get_http_config(&self) -> &Option<HTTPConfig> {
|
|
|
|
&self.http
|
|
|
|
}
|
2018-12-06 23:58:31 +00:00
|
|
|
|
|
|
|
pub fn get_db_path(&self) -> &Option<String> {
|
|
|
|
&self.db_path
|
|
|
|
}
|
2018-12-11 01:03:14 +00:00
|
|
|
|
|
|
|
pub fn get_cleanup_interval(&self) -> &Option<u64> {
|
|
|
|
&self.cleanup_interval
|
|
|
|
}
|
2018-10-20 23:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Configuration {
|
|
|
|
fn default() -> Configuration {
|
2018-12-06 23:58:31 +00:00
|
|
|
Configuration {
|
2018-10-25 20:49:25 +01:00
|
|
|
log_level: None,
|
2018-10-21 21:39:39 +01:00
|
|
|
mode: TrackerMode::DynamicMode,
|
2018-12-06 23:58:31 +00:00
|
|
|
udp: UDPConfig {
|
2018-10-21 21:39:39 +01:00
|
|
|
announce_interval: 120,
|
2018-10-20 23:57:58 +01:00
|
|
|
bind_address: String::from("0.0.0.0:6969"),
|
|
|
|
},
|
|
|
|
http: None,
|
2018-12-06 23:58:31 +00:00
|
|
|
db_path: None,
|
2018-12-11 01:03:14 +00:00
|
|
|
cleanup_interval: None,
|
2018-10-20 23:57:58 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-06 23:58:31 +00:00
|
|
|
}
|