add feature 'file' to load the config from a json file

This commit is contained in:
OMGeeky
2023-04-15 19:46:30 +02:00
parent a484c2ece4
commit 7a018f7eb8
3 changed files with 169 additions and 65 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/target /target
/Cargo.lock /Cargo.lock
/.idea

View File

@@ -1,9 +1,16 @@
[package] [package]
name = "downloader_config" name = "downloader_config"
version = "0.3.0" version = "0.3.1"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
log = "0.4.17" log = "0.4.17"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", optional = true }
[features]
default = []
file = ["dep:serde_json"]

View File

@@ -2,6 +2,7 @@ use std::env;
use std::fmt::Debug; use std::fmt::Debug;
use log::{info, trace}; use log::{info, trace};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Config { pub struct Config {
@@ -29,78 +30,173 @@ pub struct Config {
pub download_folder_path: String, pub download_folder_path: String,
} }
#[derive(Serialize, Deserialize, Default)]
struct ConfigBuilder {
pub path_auth_code: Option<String>,
pub path_authentications: Option<String>,
pub use_file_auth_response: Option<String>,
pub use_local_auth_redirect: Option<String>,
pub auth_file_read_timeout: Option<String>,
pub twitch_client_id: Option<String>,
pub twitch_client_secret: Option<String>,
pub twitch_downloader_id: Option<String>,
pub twitch_downloader_thread_count: Option<String>,
pub bigquery_project_id: Option<String>,
pub bigquery_dataset_id: Option<String>,
pub bigquery_service_account_path: Option<String>,
pub youtube_client_secret_path: Option<String>,
pub youtube_tags: Option<String>,
pub youtube_description_template: Option<String>,
pub youtube_video_length_minutes_soft_cap: Option<String>,
pub youtube_video_length_minutes_hard_cap: Option<String>,
pub download_folder_path: Option<String>,
}
pub fn load_config() -> Config { pub fn load_config() -> Config {
trace!("load_config()"); trace!("load_config()");
info!("Loading config from environment variables"); let config_builder: ConfigBuilder;
let twitch_client_id = env::var("TWITCH_CLIENT_ID").expect("TWITCH_CLIENT_ID not set"); let use_env;
let twitch_client_secret = env::var("TWITCH_CLIENT_SECRET").expect("TWITCH_CLIENT_SECRET not set");
let twitch_downloader_id = env::var("TWITCH_DOWNLOADER_ID")
.unwrap_or("kimne78kx3ncx6brgo4mv6wki5h1ko".to_string());
let twitch_downloader_thread_count = env::var("TWITCH_DOWNLOADER_THREAD_COUNT") #[cfg(not(feature = "file"))]
.unwrap_or("50".to_string()) {
.parse() use_env = true;
.expect("TWITCH_DOWNLOADER_THREAD_COUNT is not a number"); }
#[cfg(feature = "file")]
let config_file_path: Option<String>;
#[cfg(feature = "file")]
{
info!("Loading config from file");
config_file_path = env::var("CONFIG_FILE_PATH").ok();
if config_file_path.is_some() {
log::warn!("Failed to load config file path from environment variable. Using environment variables instead.");
use_env = true;
} else {
use_env = false;
}
}
if use_env {
info!("Loading config from environment variables");
config_builder = ConfigBuilder {
twitch_client_id: env::var("TWITCH_CLIENT_ID").ok(),
twitch_client_secret: env::var("TWITCH_CLIENT_SECRET").ok(),
twitch_downloader_id: env::var("TWITCH_DOWNLOADER_ID").ok(),
let path_auth_code = twitch_downloader_thread_count: env::var("TWITCH_DOWNLOADER_THREAD_COUNT").ok(),
env::var("PATH_AUTH_CODE").unwrap_or("/tmp/twba/auth/code.txt".to_string()); path_auth_code: env::var("PATH_AUTH_CODE").ok(),
let path_authentications = path_authentications: env::var("PATH_AUTHENTICATIONS").ok(),
env::var("PATH_AUTHENTICATIONS").unwrap_or("/tmp/twba/auth/{user}.json".to_string()); use_file_auth_response: env::var("USE_FILE_AUTH_RESPONSE").ok(),
let use_file_auth_response = use_local_auth_redirect: env::var("USE_LOCAL_AUTH_REDIRECT").ok(),
env::var("USE_FILE_AUTH_RESPONSE").unwrap_or("1".to_string()) == "1"; auth_file_read_timeout: env::var("AUTH_FILE_READ_TIMEOUT").ok(),
let use_local_auth_redirect =
env::var("USE_LOCAL_AUTH_REDIRECT").unwrap_or("0".to_string()) == "1";
let auth_file_read_timeout = env::var("AUTH_FILE_READ_TIMEOUT")
.unwrap_or("5".to_string())
.parse()
.unwrap();
let bigquery_project_id = bigquery_project_id: env::var("BIGQUERY_PROJECT_ID").ok(),
env::var("BIGQUERY_PROJECT_ID").unwrap_or("twitchbackup-v1".to_string()); bigquery_dataset_id: env::var("BIGQUERY_DATASET_ID").ok(),
let bigquery_dataset_id = env::var("BIGQUERY_DATASET_ID").unwrap_or("backup_data".to_string()); bigquery_service_account_path: env::var("BIGQUERY_SERVICE_ACCOUNT_PATH").ok(),
let bigquery_service_account_path = env::var("BIGQUERY_SERVICE_ACCOUNT_PATH")
.unwrap_or("auth/bigquery_service_account.json".to_string());
let youtube_client_secret_path = env::var("YOUTUBE_CLIENT_SECRET_PATH") youtube_client_secret_path: env::var("YOUTUBE_CLIENT_SECRET_PATH").ok(),
.unwrap_or("auth/youtube_client_secret.json".to_string()); youtube_tags: env::var("YOUTUBE_TAGS").ok(),
youtube_description_template: env::var("YOUTUBE_DESCRIPTION_TEMPLATE").ok(),
youtube_video_length_minutes_soft_cap: env::var(
"YOUTUBE_VIDEO_LENGTH_MINUTES_SOFT_CAP",
)
.ok(),
youtube_video_length_minutes_hard_cap: env::var(
"YOUTUBE_VIDEO_LENGTH_MINUTES_HARD_CAP",
)
.ok(),
let youtube_tags = env::var("YOUTUBE_TAGS") download_folder_path: env::var("DOWNLOAD_FOLDER_PATH").ok(),
.unwrap_or("".to_string()) };
.split(",") trace!("load_config() done loading fields");
.map(|s| s.to_string()) } else {
.collect(); #[cfg(feature = "file")]
{
let config_file_path = config_file_path.expect(
"Failed to load config file path from environment variable, \
but still ended up in the file config loading code.",
);
let config_file = std::fs::read_to_string(&config_file_path).expect(&format!(
"Failed to read config file at path: {}",
config_file_path
));
config_builder =
serde_json::from_str(&config_file).expect("Failed to parse config file");
trace!("load_config() done loading fields from file");
}
#[cfg(not(feature = "file"))]
panic!("Failed to load config from file and environment variables");
}
let youtube_description_template =
env::var("YOUTUBE_DESCRIPTION_TEMPLATE").unwrap_or("test description for \"$$video_title$$\"".to_string());
let youtube_video_length_minutes_soft_cap =
env::var("YOUTUBE_VIDEO_LENGTH_MINUTES_SOFT_CAP").unwrap_or("300".to_string()).parse().unwrap_or(30i64);
let youtube_video_length_minutes_hard_cap =
env::var("YOUTUBE_VIDEO_LENGTH_MINUTES_HARD_CAP").unwrap_or("359".to_string()).parse().unwrap_or(30i64);
let download_folder_path =
env::var("DOWNLOAD_FOLDER_PATH").unwrap_or("/tmp/twba/videos/".to_string());
trace!("load_config() done loading fields");
Config { Config {
path_auth_code, path_auth_code: config_builder
use_file_auth_response, .path_auth_code
path_authentications, .unwrap_or("/tmp/twba/auth/code.txt".to_string()),
use_local_auth_redirect, use_file_auth_response: config_builder
auth_file_read_timeout, .use_file_auth_response
twitch_client_id, .unwrap_or("1".to_string())
twitch_client_secret, == "1",
twitch_downloader_id, path_authentications: config_builder
twitch_downloader_thread_count, .path_authentications
bigquery_project_id, .unwrap_or("/tmp/twba/auth/{user}.json".to_string()),
bigquery_dataset_id, use_local_auth_redirect: config_builder
bigquery_service_account_path, .use_local_auth_redirect
youtube_client_secret_path, .unwrap_or("0".to_string())
youtube_tags, == "1",
youtube_description_template, auth_file_read_timeout: config_builder
youtube_video_length_minutes_soft_cap, .auth_file_read_timeout
youtube_video_length_minutes_hard_cap, .unwrap_or("5".to_string())
download_folder_path, .parse()
.expect("AUTH_FILE_READ_TIMEOUT is not a number"),
twitch_client_id: config_builder
.twitch_client_id
.expect("TWITCH_CLIENT_ID not set"),
twitch_client_secret: config_builder
.twitch_client_secret
.expect("TWITCH_CLIENT_SECRET not set"),
twitch_downloader_id: config_builder
.twitch_downloader_id
.unwrap_or("kimne78kx3ncx6brgo4mv6wki5h1ko".to_string()),
twitch_downloader_thread_count: config_builder
.twitch_downloader_thread_count
.unwrap_or("50".to_string())
.parse()
.expect("TWITCH_DOWNLOADER_THREAD_COUNT is not a number"),
bigquery_project_id: config_builder
.bigquery_project_id
.unwrap_or("twitchbackup-v1".to_string()),
bigquery_dataset_id: config_builder
.bigquery_dataset_id
.unwrap_or("backup_data".to_string()),
bigquery_service_account_path: config_builder
.bigquery_service_account_path
.unwrap_or("auth/bigquery_service_account.json".to_string()),
youtube_client_secret_path: config_builder
.youtube_client_secret_path
.unwrap_or("auth/youtube_client_secret.json".to_string()),
youtube_tags: config_builder
.youtube_tags
.unwrap_or("".to_string())
.split(",")
.map(|s| s.to_string())
.collect(),
youtube_description_template: config_builder
.youtube_description_template
.unwrap_or("test description for \"$$video_title$$\"".to_string()),
youtube_video_length_minutes_soft_cap: config_builder
.youtube_video_length_minutes_soft_cap
.unwrap_or("300".to_string())
.parse()
.unwrap_or(30i64),
youtube_video_length_minutes_hard_cap: config_builder
.youtube_video_length_minutes_hard_cap
.unwrap_or("359".to_string())
.parse()
.unwrap_or(60i64),
download_folder_path: config_builder
.download_folder_path
.unwrap_or("/tmp/twba/videos/".to_string()),
} }
} }