adding configuration parsing
This commit is contained in:
parent
70ecfdb6ac
commit
f5fe87631c
|
@ -12,4 +12,5 @@ serde = "1.0.80"
|
|||
bincode = "1.0.1"
|
||||
serde_derive = "1.0.80"
|
||||
actix-web = "0.7.13"
|
||||
binascii = "0.1.2"
|
||||
binascii = "0.1.2"
|
||||
toml = "0.4.8"
|
||||
|
|
71
src/config.rs
Normal file
71
src/config.rs
Normal 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,
|
||||
}
|
||||
}
|
||||
}
|
12
src/main.rs
12
src/main.rs
|
@ -3,13 +3,25 @@ extern crate serde;
|
|||
#[macro_use] extern crate serde_derive;
|
||||
extern crate actix_web;
|
||||
extern crate binascii;
|
||||
extern crate toml;
|
||||
|
||||
mod server;
|
||||
mod tracker;
|
||||
mod stackvec;
|
||||
mod webserver;
|
||||
mod config;
|
||||
use config::Configuration;
|
||||
|
||||
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());
|
||||
|
||||
// start http server:
|
||||
|
|
|
@ -2,15 +2,19 @@ use std;
|
|||
|
||||
use server::Events;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub enum TrackerMode {
|
||||
|
||||
/// In static mode torrents are tracked only if they were added ahead of time.
|
||||
#[serde(rename="static")]
|
||||
StaticMode,
|
||||
|
||||
/// In dynamic mode, torrents are tracked being added ahead of time.
|
||||
#[serde(rename="dynamic")]
|
||||
DynamicMode,
|
||||
|
||||
/// Tracker will only serve authenticated peers.
|
||||
#[serde(rename="private")]
|
||||
PrivateMode,
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue