main
_ 2020-11-05 22:18:47 -06:00
parent 3a50424a35
commit 26b4c335c6
4 changed files with 36 additions and 28 deletions

25
src/bin/load_toml.rs Normal file
View File

@ -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 <Path> + 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))
}
}

View File

@ -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 <dyn Error>> {
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);

View File

@ -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 <dyn Error>> {
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
}

View File

@ -21,7 +21,7 @@ use hyper::{
};
use hyper::service::{make_service_fn, service_fn};
use serde::{
Deserialize,
Deserialize,
Serialize,
};
use tokio::{