adding configuration parsing

This commit is contained in:
Naim A 2018-10-21 01:57:58 +03:00
parent 70ecfdb6ac
commit f5fe87631c
No known key found for this signature in database
GPG key ID: FD7948915D9EF8B9
4 changed files with 89 additions and 1 deletions

View file

@ -13,3 +13,4 @@ bincode = "1.0.1"
serde_derive = "1.0.80" serde_derive = "1.0.80"
actix-web = "0.7.13" actix-web = "0.7.13"
binascii = "0.1.2" binascii = "0.1.2"
toml = "0.4.8"

71
src/config.rs Normal file
View file

@ -0,0 +1,71 @@
use std;
use std::collections::HashMap;
use toml;
use serde;
pub use tracker::TrackerMode;
#[derive(Deserialize)]
pub struct UDPConfig {
bind_address: String,
mode: TrackerMode,
}
#[derive(Deserialize)]
pub struct HTTPConfig {
bind_address: String,
access_tokens: HashMap<String, String>,
}
#[derive(Deserialize)]
pub struct Configuration {
udp: UDPConfig,
http: Option<HTTPConfig>,
}
#[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 {
use std::fmt::Display;
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)),
Ok(data) => {
match Self::load(data.as_slice()) {
Ok(cfg) => Ok(cfg),
Err(e) => Err(ConfigError::ParseError(e)),
}
}
}
}
}
impl Default for Configuration {
fn default() -> Configuration {
Configuration{
udp: UDPConfig{
bind_address: String::from("0.0.0.0:6969"),
mode: TrackerMode::DynamicMode,
},
http: None,
}
}
}

View file

@ -3,13 +3,25 @@ extern crate serde;
#[macro_use] extern crate serde_derive; #[macro_use] extern crate serde_derive;
extern crate actix_web; extern crate actix_web;
extern crate binascii; extern crate binascii;
extern crate toml;
mod server; mod server;
mod tracker; mod tracker;
mod stackvec; mod stackvec;
mod webserver; mod webserver;
mod config;
use config::Configuration;
fn main() { fn main() {
let cfg = match Configuration::load_file("udpt.toml") {
Ok(v) => v,
Err(e) => {
eprintln!("failed to open configuration: {}", e);
return;
}
};
let tracker = std::sync::Arc::new(tracker::TorrentTracker::new()); let tracker = std::sync::Arc::new(tracker::TorrentTracker::new());
// start http server: // start http server:

View file

@ -2,15 +2,19 @@ use std;
use server::Events; use server::Events;
#[derive(Deserialize)]
pub enum TrackerMode { pub enum TrackerMode {
/// In static mode torrents are tracked only if they were added ahead of time. /// In static mode torrents are tracked only if they were added ahead of time.
#[serde(rename="static")]
StaticMode, StaticMode,
/// In dynamic mode, torrents are tracked being added ahead of time. /// In dynamic mode, torrents are tracked being added ahead of time.
#[serde(rename="dynamic")]
DynamicMode, DynamicMode,
/// Tracker will only serve authenticated peers. /// Tracker will only serve authenticated peers.
#[serde(rename="private")]
PrivateMode, PrivateMode,
} }