diff options
| author | Fuwn <[email protected]> | 2021-04-27 17:02:09 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-04-27 17:02:09 -0700 |
| commit | 64cb1207fac4a86bebae1ea1cd64547fa7a5b22b (patch) | |
| tree | 2d0812b3398663c52e7824bb38e51acd6ed6f578 | |
| parent | etc: Add a Say Thanks button :) (diff) | |
| download | whirl-64cb1207fac4a86bebae1ea1cd64547fa7a5b22b.tar.xz whirl-64cb1207fac4a86bebae1ea1cd64547fa7a5b22b.zip | |
feature: New configuration file format
| -rw-r--r-- | .gitignore | 10 | ||||
| -rw-r--r-- | .whirlrc.toml.example | 4 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | Whirl.toml.example | 10 | ||||
| -rw-r--r-- | src/config.rs | 32 | ||||
| -rw-r--r-- | src/config/Whirl.default.toml | 10 | ||||
| -rw-r--r-- | src/config/mod.rs | 38 | ||||
| -rw-r--r-- | src/main.rs | 16 | ||||
| -rw-r--r-- | src/server/cmd/commands/property/create.rs | 8 | ||||
| -rw-r--r-- | src/server/cmd/commands/room/create.rs | 4 | ||||
| -rw-r--r-- | src/server/distributor.rs | 6 | ||||
| -rw-r--r-- | src/server/hub.rs | 6 |
12 files changed, 84 insertions, 62 deletions
@@ -11,16 +11,16 @@ Cargo.lock # IDE /.idea/ -whirl.iml +/whirl.iml # Development /src/_*.* -whirl.db -.whirlrc.toml +/whirl.db +/Whirl.toml # DB Browser -whirl.db-shm -whirl.db-wal +/whirl.db-shm +/whirl.db-wal # Wireshark *.pcapng diff --git a/.whirlrc.toml.example b/.whirlrc.toml.example deleted file mode 100644 index aaeb572..0000000 --- a/.whirlrc.toml.example +++ /dev/null @@ -1,4 +0,0 @@ -worldsmaster_greeting = 'Welcome to Whirlsplash!' -worldsmaster_username = "WORLDSMASTER" # DO NOT CHANGE UNLESS YOU KNOW WHAT YOU ARE DOING -distributor_port = 6650 # DO NOT CHANGE UNLESS YOU KNOW WHAT YOU ARE DOING -hub_port = 5673 # DO NOT CHANGE UNLESS YOU KNOW WHAT YOU ARE DOING @@ -37,7 +37,7 @@ byteorder = "1.4.3" structopt = "0.3.21" # Config -confy = "0.4.0" +config = "0.11.0" # TCP tokio = { version = "1.4.0", features = ["full"] } diff --git a/Whirl.toml.example b/Whirl.toml.example new file mode 100644 index 0000000..688fa88 --- /dev/null +++ b/Whirl.toml.example @@ -0,0 +1,10 @@ +[whirlsplash] +worldsmaster_username = "WORLDSMASTER" # DO NOT CHANGE UNLESS YOU KNOW WHAT YOU ARE DOING +log_level = 1 # Generally, you should only change this if you are expiriencing issues + +[distributor] +worldsmaster_greeting = "Welcome to Whirlsplash!" +port = 6650 # DO NOT CHANGE UNLESS YOU KNOW WHAT YOU ARE DOING + +[hub] +port = 5673 # DO NOT CHANGE UNLESS YOU KNOW WHAT YOU ARE DOING diff --git a/src/config.rs b/src/config.rs deleted file mode 100644 index d1a49ce..0000000 --- a/src/config.rs +++ /dev/null @@ -1,32 +0,0 @@ -// Copyleft 2021-2021 Whirlsplash -// SPDX-License-Identifier: GPL-3.0-only - -use serde_derive::{Deserialize, Serialize}; - -#[derive(Debug, Serialize, Deserialize)] -pub struct Config { - pub worldsmaster_greeting: String, - pub worldsmaster_username: String, - pub distributor_port: i32, - pub hub_port: i32, -} -impl Default for Config { - fn default() -> Self { - Config { - worldsmaster_greeting: "Welcome to Whirlsplash!".to_string(), - worldsmaster_username: "WORLDSMASTER".to_string(), - distributor_port: 6650, - hub_port: 5673, - } - } -} - -pub fn get_config() -> Result<Config, confy::ConfyError> { - let config: Config = confy::load_path("./.whirlrc.toml").unwrap(); - - Ok(config) -} - -pub fn store_config(config: Config) -> Result<(), confy::ConfyError> { - confy::store_path("./.whirlrc.toml", config) -} diff --git a/src/config/Whirl.default.toml b/src/config/Whirl.default.toml new file mode 100644 index 0000000..688fa88 --- /dev/null +++ b/src/config/Whirl.default.toml @@ -0,0 +1,10 @@ +[whirlsplash] +worldsmaster_username = "WORLDSMASTER" # DO NOT CHANGE UNLESS YOU KNOW WHAT YOU ARE DOING +log_level = 1 # Generally, you should only change this if you are expiriencing issues + +[distributor] +worldsmaster_greeting = "Welcome to Whirlsplash!" +port = 6650 # DO NOT CHANGE UNLESS YOU KNOW WHAT YOU ARE DOING + +[hub] +port = 5673 # DO NOT CHANGE UNLESS YOU KNOW WHAT YOU ARE DOING diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..f57335d --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,38 @@ +// Copyleft 2021-2021 Whirlsplash +// SPDX-License-Identifier: GPL-3.0-only + +use config::{ConfigError, File}; +use serde::Deserialize; +use serde_derive::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +pub struct WhirlsplashConfig { + pub worldsmaster_username: String, + pub log_level: i64, +} +#[derive(Serialize, Deserialize, Debug)] +pub struct DistributorConfig { + pub worldsmaster_greeting: String, + pub port: i64, +} +#[derive(Serialize, Deserialize, Debug)] +pub struct HubConfig { + pub port: i64, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Config { + pub whirlsplash: WhirlsplashConfig, + pub distributor: DistributorConfig, + pub hub: HubConfig, +} +impl Config { + fn load() -> Result<Self, ConfigError> { + let mut s = config::Config::new(); + + s.merge(File::with_name("./Whirl.toml").required(false))?; + s.try_into() + } + + pub fn get() -> Result<Self, ConfigError> { Self::load() } +} diff --git a/src/main.rs b/src/main.rs index 3113704..82dcfb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,8 +9,7 @@ use std::error::Error; use structopt::clap::Shell; use whirl::{ cli::cli, - config, - config::get_config, + config::Config, server::{ distributor::Distributor, hub::Hub, @@ -20,6 +19,7 @@ use whirl::{ }, }, }; +use log::LevelFilter; #[tokio::main] async fn main() -> Result<(), Box<dyn Error>> { @@ -27,11 +27,11 @@ async fn main() -> Result<(), Box<dyn Error>> { let matches = cli().get_matches(); // Set logging level - let mut log_level = "whirl=error,whirl=warn,whirl=info".to_string(); - if matches.is_present("debug") { + let mut log_level = "whirl=error,whirl=warn,whirl=trace".to_string(); + if matches.is_present("debug") || Config::get()?.whirlsplash.log_level >= 2 { log_level += ",whirl=debug"; } - if matches.is_present("trace") { + if matches.is_present("trace") || Config::get()?.whirlsplash.log_level >= 3 { log_level += ",whirl=trace"; } std::env::set_var("RUST_LOG", log_level); @@ -48,7 +48,7 @@ async fn main() -> Result<(), Box<dyn Error>> { run().await.unwrap(); } else if let Some(cmd) = matches.subcommand_matches("config") { if cmd.is_present("show") { - println!("{:#?}", config::get_config()); + println!("{:#?}", Config::get()?); } } else if let Some(shell) = matches.subcommand_matches("completions") { if shell.is_present("powershell") { @@ -72,14 +72,14 @@ async fn run() -> Result<(), Box<dyn Error>> { let threads = vec![ tokio::spawn(async move { let _ = Distributor::listen( - &*format!("0.0.0.0:{}", get_config().unwrap().distributor_port,), + &*format!("0.0.0.0:{}", Config::get().unwrap().distributor.port), AutoServer, ) .await; }), tokio::spawn(async move { let _ = Hub::listen( - &*format!("0.0.0.0:{}", get_config().unwrap().hub_port), + &*format!("0.0.0.0:{}", Config::get().unwrap().hub.port), RoomServer, ) .await; diff --git a/src/server/cmd/commands/property/create.rs b/src/server/cmd/commands/property/create.rs index 8521a8e..948f434 100644 --- a/src/server/cmd/commands/property/create.rs +++ b/src/server/cmd/commands/property/create.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: GPL-3.0-only use crate::{ - config::get_config, + config::Config, server::{ cmd::constants::{PROPUPD, SESSINIT}, net::{ @@ -57,7 +57,7 @@ pub fn create_property_update_as_distributor() -> Vec<u8> { }, NetworkProperty { prop_id: VAR_APPNAME, - value: get_config().unwrap().worldsmaster_username, + value: Config::get().unwrap().whirlsplash.worldsmaster_username, }, ], ) @@ -98,7 +98,7 @@ pub fn create_property_update_as_hub() -> Vec<u8> { }, NetworkProperty { prop_id: VAR_APPNAME, - value: get_config().unwrap().worldsmaster_username, + value: Config::get().unwrap().whirlsplash.worldsmaster_username, }, ], ) @@ -115,7 +115,7 @@ pub fn create_property_request_as_distributor() -> Vec<u8> { }, NetworkProperty { prop_id: VAR_APPNAME, - value: get_config().unwrap().worldsmaster_username, + value: Config::get().unwrap().whirlsplash.worldsmaster_username, }, NetworkProperty { prop_id: VAR_PROTOCOL, diff --git a/src/server/cmd/commands/room/create.rs b/src/server/cmd/commands/room/create.rs index a195936..598d39b 100644 --- a/src/server/cmd/commands/room/create.rs +++ b/src/server/cmd/commands/room/create.rs @@ -3,7 +3,7 @@ use bytes::{BufMut, BytesMut}; -use crate::{config::get_config, server::cmd::constants::REDIRID}; +use crate::{config::Config, server::cmd::constants::REDIRID}; pub fn create_room_id_request(room: &str, room_id: u8) -> Vec<u8> { let mut command = BytesMut::new(); @@ -23,7 +23,7 @@ pub fn create_room_id_request(room: &str, room_id: u8) -> Vec<u8> { for byte in "0.0.0.0".split('.') { command.put_u8(byte.parse::<u8>().unwrap()); } - command.put_u16(get_config().unwrap().hub_port as u16); // Port + command.put_u16(Config::get().unwrap().hub.port as u16); // Port // Length let mut command_as_vec = command.to_vec(); diff --git a/src/server/distributor.rs b/src/server/distributor.rs index 4e7a694..fe3bea7 100644 --- a/src/server/distributor.rs +++ b/src/server/distributor.rs @@ -18,7 +18,7 @@ use tokio_stream::StreamExt; use tokio_util::codec::{BytesCodec, Decoder}; use crate::{ - config::get_config, + config::Config, server::{ cmd::{ commands::{ @@ -87,8 +87,8 @@ impl Server for Distributor { peer.bytes.get_mut() .write_all(&create_text(Text { - sender: get_config()?.worldsmaster_username, - content: get_config()?.worldsmaster_greeting, + sender: Config::get()?.whirlsplash.worldsmaster_username, + content: Config::get()?.distributor.worldsmaster_greeting, })).await?; peer.bytes.get_mut() .write_all(&create_action()).await?; diff --git a/src/server/hub.rs b/src/server/hub.rs index 6a1480f..7513f12 100644 --- a/src/server/hub.rs +++ b/src/server/hub.rs @@ -14,7 +14,7 @@ use tokio_stream::StreamExt; use tokio_util::codec::{BytesCodec, Decoder}; use crate::{ - config::get_config, + config::Config, server::{ cmd::{ commands::{ @@ -85,8 +85,8 @@ impl Server for Hub { peer.bytes.get_mut() .write_all(&create_text(Text { - sender: get_config()?.worldsmaster_username, - content: get_config()?.worldsmaster_greeting, + sender: Config::get()?.whirlsplash.worldsmaster_username, + content: Config::get()?.distributor.worldsmaster_greeting, })).await?; peer.bytes.get_mut() .write_all(&create_action()).await?; |