diff --git a/src/bin/load_toml.rs b/src/bin/load_toml.rs new file mode 100644 index 0000000..acbb64d --- /dev/null +++ b/src/bin/load_toml.rs @@ -0,0 +1,25 @@ +use std::{ + fmt::Debug, + fs::File, + io::Read, + path::Path, +}; + +use serde::de::DeserializeOwned; + +pub fn load < + T: DeserializeOwned, + P: AsRef + Debug +> ( + config_file_path: P +) -> T { + let mut f = File::open (&config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path)); + let mut buffer = vec! [0u8; 4096]; + let bytes_read = f.read (&mut buffer).unwrap_or_else (|_| panic! ("Can't read {:?}", config_file_path)); + buffer.truncate (bytes_read); + + { + let config_s = String::from_utf8 (buffer).unwrap_or_else (|_| panic! ("Can't parse {:?} as UTF-8", config_file_path)); + toml::from_str (&config_s).unwrap_or_else (|_| panic! ("Can't parse {:?} as TOML", config_file_path)) + } +} diff --git a/src/bin/ptth_relay.rs b/src/bin/ptth_relay.rs index 45683c7..92f851e 100644 --- a/src/bin/ptth_relay.rs +++ b/src/bin/ptth_relay.rs @@ -6,24 +6,14 @@ use std::{ use tokio::sync::oneshot; +mod load_toml; + use ptth::relay; use ptth::relay::RelayState; #[tokio::main] async fn main () -> Result <(), Box > { - use std::io::Read; - - let config_file = { - let config_file_path = "config/ptth_relay.toml"; - - let mut f = File::open (config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path)); - let mut buffer = vec! [0u8; 4096]; - let bytes_read = f.read (&mut buffer).unwrap_or_else (|_| panic! ("Can't read {:?}", config_file_path)); - buffer.truncate (bytes_read); - - let config_s = String::from_utf8 (buffer).unwrap_or_else (|_| panic! ("Can't parse {:?} as UTF-8", config_file_path)); - toml::from_str (&config_s).unwrap_or_else (|_| panic! ("Can't parse {:?} as TOML", config_file_path)) - }; + let config_file = load_toml::load ("config/ptth_relay.toml"); eprintln! ("ptth_relay Git version: {:?}", ptth::git_version::GIT_VERSION); diff --git a/src/bin/ptth_server.rs b/src/bin/ptth_server.rs index 63370de..fa98799 100644 --- a/src/bin/ptth_server.rs +++ b/src/bin/ptth_server.rs @@ -5,6 +5,8 @@ use std::{ use structopt::StructOpt; +mod load_toml; + #[derive (Debug, StructOpt)] struct Opt { #[structopt (long)] @@ -13,19 +15,10 @@ struct Opt { #[tokio::main] async fn main () -> Result <(), Box > { - use std::io::Read; + let config_file = load_toml::load ("config/ptth_server.toml"); - let config_file = { - let config_file_path = "config/ptth_server.toml"; - - let mut f = std::fs::File::open (config_file_path).unwrap_or_else (|_| panic! ("Can't open {:?}", config_file_path)); - let mut buffer = vec! [0u8; 4096]; - let bytes_read = f.read (&mut buffer).unwrap_or_else (|_| panic! ("Can't read {:?}", config_file_path)); - buffer.truncate (bytes_read); - - let config_s = String::from_utf8 (buffer).unwrap_or_else (|_| panic! ("Can't parse {:?} as UTF-8", config_file_path)); - toml::from_str (&config_s).unwrap_or_else (|_| panic! ("Can't parse {:?} as TOML", config_file_path)) - }; - - ptth::server::run_server (config_file, None).await + ptth::server::run_server ( + config_file, + None + ).await } diff --git a/src/relay/mod.rs b/src/relay/mod.rs index ef95378..d615a80 100644 --- a/src/relay/mod.rs +++ b/src/relay/mod.rs @@ -21,7 +21,7 @@ use hyper::{ }; use hyper::service::{make_service_fn, service_fn}; use serde::{ - Deserialize, + Deserialize, Serialize, }; use tokio::{