switch to env-based config

This commit is contained in:
Brandon Thomas committed 2021-05-12 00:58:55 -04:00
1 parent fc05752ce1
commit ac7d4db4a3
2 files changed
+90 -4

No files matched your search

+64 -4
View File
@@ -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<T> = anyhow::Result<T>;
// 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<RedisSecrets> {
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::<u32>(
ENV_REDIS_PORT, ENV_REDIS_PORT_DEFAULT)?;
Ok(RedisSecrets::new(
&redis_username,
&redis_password,
&redis_hostname,
redis_port
))
}
fn get_twitch_secrets() -> AnyhowResult<TwitchSecrets> {
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,
))
}
+26
View File
@@ -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<String>) -> 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<UserConfig> {
let config = UserConfig::builder()
.name(self.username.to_string())