diff options
| author | Fuwn <[email protected]> | 2021-03-29 13:31:02 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-03-29 13:31:02 -0700 |
| commit | 12517eee14d79a2673338c94cbc0d091840b32a3 (patch) | |
| tree | e6092233c3236060974a35614861b20ef9bca88a /src | |
| parent | feature: Implement CLI (diff) | |
| download | whirl-12517eee14d79a2673338c94cbc0d091840b32a3.tar.xz whirl-12517eee14d79a2673338c94cbc0d091840b32a3.zip | |
feature: Use TOML as config instead of environment variables
Diffstat (limited to 'src')
| -rw-r--r-- | src/cli.rs | 25 | ||||
| -rw-r--r-- | src/config.rs | 23 | ||||
| -rw-r--r-- | src/lib.rs | 4 | ||||
| -rw-r--r-- | src/main.rs | 31 | ||||
| -rw-r--r-- | src/server/auto/server.rs | 3 |
5 files changed, 60 insertions, 26 deletions
diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 0000000..f59139d --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,25 @@ +use structopt::StructOpt; + +#[derive(StructOpt, Debug)] +pub enum Command { + Run, + Config, +} + +#[derive(StructOpt, Debug)] +#[structopt( +name = env!("CARGO_PKG_NAME"), +about = env!("CARGO_PKG_DESCRIPTION"), +version = env!("CARGO_PKG_VERSION"), +author = env!("CARGO_PKG_AUTHORS"), +)] +pub struct Opt { + #[structopt(short, long)] + pub debug: bool, + + #[structopt(short, long, parse(from_occurrences))] + pub verbose: u8, + + #[structopt(subcommand)] // help + pub command: Command, +} diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..13ffa61 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,23 @@ +use serde_derive::{Serialize, Deserialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Config { + pub worldsmaster_greeting: String, +} +impl Default for Config { + fn default() -> Self { + Config { + worldsmaster_greeting: "Welcome to Whirlsplash!".to_string(), + } + } +} + +pub fn get_config() -> Result<Config, confy::ConfyError> { + let config: Config = confy::load_path("./whirl.toml").unwrap(); + + Ok(config) +} + +pub fn store_config(config: Config) -> Result<(), confy::ConfyError> { + confy::store_path("./whirl.toml", config) +} @@ -6,4 +6,6 @@ extern crate log; pub mod db; pub mod server; -pub mod utils;
\ No newline at end of file +pub mod utils; +pub mod cli; +pub mod config; diff --git a/src/main.rs b/src/main.rs index 5489024..88bc81c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,33 +2,12 @@ use whirl::server::auto::server::AutoServer; use std::error::Error; use whirl::server::room::server::RoomServer; use structopt::StructOpt; - -#[derive(StructOpt, Debug)] -enum Command { - Run, -} - -#[derive(StructOpt, Debug)] -#[structopt( - name = env!("CARGO_PKG_NAME"), - about = env!("CARGO_PKG_DESCRIPTION"), - version = env!("CARGO_PKG_VERSION"), - author = env!("CARGO_PKG_AUTHORS"), -)] -struct Opt { - #[structopt(short, long)] - debug: bool, - - #[structopt(short, long, parse(from_occurrences))] - verbose: u8, - - #[structopt(subcommand)] // help - command: Command, -} +use whirl::config; +use whirl::cli::Command; #[tokio::main] async fn main() -> Result<(), Box<dyn Error>> { - let opt = Opt::from_args(); + let opt = whirl::cli::Opt::from_args(); // Set logging level let mut log_level = "whirl=error,whirl=warn,whirl=info".to_string(); @@ -36,6 +15,9 @@ async fn main() -> Result<(), Box<dyn Error>> { if opt.verbose >= 1 { log_level += ",whirl=trace"; }; std::env::set_var("RUST_LOG", log_level); + // Set database URL + std::env::set_var("DATABASE_URL", "sqlite:worlds.db"); + // Setup logging dotenv::dotenv().ok(); pretty_env_logger::init(); @@ -43,6 +25,7 @@ async fn main() -> Result<(), Box<dyn Error>> { // Handle CLI command match opt.command { Command::Run => run().await, + Command::Config => println!("{:#?}", config::get_config()), } Ok(()) diff --git a/src/server/auto/server.rs b/src/server/auto/server.rs index a2d5609..7f5781f 100644 --- a/src/server/auto/server.rs +++ b/src/server/auto/server.rs @@ -19,6 +19,7 @@ use std::net::SocketAddr; use crate::server::auto::cmd::session::parse_session_initialization_command; use crate::server::parser::get_commands_from_buffer; use crate::server::cmd::property::parse_property_set_command; +use crate::config::get_config; pub struct AutoServer; impl AutoServer { @@ -90,7 +91,7 @@ impl AutoServer { debug!("received property set command from client: {}", avatar); peer.bytes.get_mut() .write_all(&create_text_command_with_action( - "WORLDSMASTER", &std::env::var("WORLDSMASTER_GREETING")? + "WORLDSMASTER", &get_config()?.worldsmaster_greeting )).await?; debug!("sent worldsmaster greeting to client"); } |