From ac7d4db4a32c6cc1c396da4311957adbfcda8e24 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 12 May 2021 00:58:55 -0400 Subject: [PATCH] switch to env-based config --- src/main.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++--- src/secrets.rs | 26 +++++++++++++++++++ 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index aa17ac02aa..2abb45e923 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ use anyhow::anyhow; use anyhow::{Context, Error}; use crate::clients::redis_client::RedisClient; use crate::clients::twitch_client::TwitchClient; -use crate::secrets::Secrets; +use crate::secrets::{Secrets, RedisSecrets, TwitchSecrets}; use log::{info, warn}; use redis::aio::Connection; use redis::{AsyncCommands, RedisResult}; @@ -32,18 +32,38 @@ use std::time::Duration; pub type AnyhowResult = anyhow::Result; +// Redis connection + +const ENV_REDIS_USERNAME : &'static str = "REDIS_USERNAME"; +const ENV_REDIS_USERNAME_DEFAULT : &'static str = "root"; + +const ENV_REDIS_PASSWORD : &'static str = "REDIS_PASSWORD"; +const ENV_REDIS_PASSWORD_DEFAULT : &'static str = ""; + +const ENV_REDIS_HOST: &'static str = "REDIS_HOST"; +const ENV_REDIS_HOST_DEFAULT: &'static str = "localhost"; + +const ENV_REDIS_PORT: &'static str = "REDIS_PORT"; +const ENV_REDIS_PORT_DEFAULT: u32 = 6379; + +// Redis publishing + const ENV_PUBLISH_TOPIC : &'static str = "PUBLISH_TOPIC"; const ENV_PUBLISH_TOPIC_DEFAULT : &'static str = "firehose"; const ENV_REDIS_MAX_RETRY_COUNT : &'static str = "REDIS_MAX_RETRY_COUNT"; const ENV_REDIS_MAX_RETRY_COUNT_DEFAULT : u32 = 3; + #[tokio::main] async fn main() -> anyhow::Result<()> { easyenv::init_env_logger(None); info!("Reading env configs..."); + let redis_secrets = get_redis_secrets()?; + let twitch_secrets = get_twitch_secrets()?; + let redis_publish_topic = easyenv::get_env_string_or_default( ENV_PUBLISH_TOPIC, ENV_PUBLISH_TOPIC_DEFAULT); @@ -51,10 +71,10 @@ async fn main() -> anyhow::Result<()> { ENV_REDIS_MAX_RETRY_COUNT, ENV_REDIS_MAX_RETRY_COUNT_DEFAULT)?; - let secrets = Secrets::from_file("secrets.toml")?; + //let secrets = Secrets::from_file("secrets.toml")?; let mut redis_client = RedisClient::new( - &secrets.redis, + &redis_secrets, redis_max_retry_count ); @@ -63,7 +83,7 @@ async fn main() -> anyhow::Result<()> { redis_client.connect().await?; let mut twitch_client = TwitchClient::new( - &secrets.twitch, + &twitch_secrets, redis_client, &redis_publish_topic ); @@ -76,3 +96,43 @@ async fn main() -> anyhow::Result<()> { warn!("Restarting Twitch client..."); } } + +fn get_redis_secrets() -> AnyhowResult { + let redis_username = easyenv::get_env_string_or_default( + ENV_REDIS_USERNAME, ENV_REDIS_USERNAME_DEFAULT); + + let redis_password = easyenv::get_env_string_or_default( + ENV_REDIS_PASSWORD, ENV_REDIS_PASSWORD_DEFAULT); + + let redis_hostname = easyenv::get_env_string_or_default( + ENV_REDIS_HOST, ENV_REDIS_HOST_DEFAULT); + + let redis_port = easyenv::get_env_num::( + ENV_REDIS_PORT, ENV_REDIS_PORT_DEFAULT)?; + + Ok(RedisSecrets::new( + &redis_username, + &redis_password, + &redis_hostname, + redis_port + )) +} + +fn get_twitch_secrets() -> AnyhowResult { + let watch_channels = vec![ + "#vocodes", + "#testytest512", + "#ech310n", + ].iter() + .map(|s| s.to_string()) + .collect(); + + Ok(TwitchSecrets::new( + &easyenv::get_env_string_required("TWITCH_USERNAME")?, + &easyenv::get_env_string_required("TWITCH_STREAM_KEY")?, + &easyenv::get_env_string_required("TWITCH_APP_CLIENT_ID")?, + &easyenv::get_env_string_required("TWITCH_APP_CLIENT_SECRET")?, + &easyenv::get_env_string_required("TWITCH_OAUTH_ACCESS_TOKEN")?, + &watch_channels, + )) +} diff --git a/src/secrets.rs b/src/secrets.rs index a60ae93e3b..f4eac0c7fb 100644 --- a/src/secrets.rs +++ b/src/secrets.rs @@ -58,12 +58,38 @@ impl Secrets { } impl RedisSecrets { + pub fn new(username: &str, password: &str, host: &str, port: u32) -> Self { + Self { + username: username.to_string(), + password: password.to_string(), + host: host.to_string(), + port, + } + } + pub fn connection_url(&self) -> String { format!("rediss://{}:{}@{}:{}", self.username, self.password, self.host, self.port) } } impl TwitchSecrets { + pub fn new(username: &str, + stream_key: &str, + application_client_id: &str, + application_client_secret: &str, + oauth_access_token: &str, + watch_channels: &Vec) -> Self { + + Self { + username: username.to_string(), + stream_key: stream_key.to_string(), + application_client_id: application_client_id.to_string(), + application_client_secret: application_client_secret.to_string(), + oauth_access_token: oauth_access_token.to_string(), + watch_channels: watch_channels.clone(), + } + } + pub fn get_user_config(&self) -> AnyhowResult { let config = UserConfig::builder() .name(self.username.to_string())