From 9949dc7124162e23e509ed73b0c8fb7bab3b5722 Mon Sep 17 00:00:00 2001 From: thevickypedia Date: Sun, 25 Feb 2024 17:35:47 -0600 Subject: [PATCH] Allow passing `env_file` as an argument Add more file extensions for images --- Cargo.toml | 2 +- README.md | 4 ++-- src/lib.rs | 2 +- src/main.rs | 4 ++-- src/routes/media.rs | 2 +- src/squire/mod.rs | 2 ++ src/squire/parser.rs | 51 +++++++++++++++++++++++++++++++++++++++++++ src/squire/startup.rs | 11 ++++++++-- 8 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 src/squire/parser.rs diff --git a/Cargo.toml b/Cargo.toml index 4064b06..75f8f0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "RuStream" -version = "1.0.1" +version = "1.1.0-a" description = "Self-hosted Streaming Engine, that can render media files via authenticated sessions." license = "MIT" documentation = "https://docs.rs/RuStream" diff --git a/README.md b/README.md index cadf9a6..d5cf196 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ use rustream; async fn main() { match rustream::start().await { Ok(_) => { - println!("RuStream session terminated") + println!("RuStream session has ended") } Err(err) => { - eprintln!("Error starting rustream: {}", err) + eprintln!("Error starting RuStream: {}", err) } } } diff --git a/src/lib.rs b/src/lib.rs index 7fc68d3..1e90ec9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ mod templates; /// ``` pub async fn start() -> io::Result<()> { let cargo = constant::build_info(); - let config = squire::startup::get_config(); + let config = squire::startup::get_config(&cargo); squire::startup::init_logger(config.debug, &cargo.crate_name); println!("{}[v{}] - {}", &cargo.pkg_name, &cargo.pkg_version, &cargo.description); diff --git a/src/main.rs b/src/main.rs index 4f825b2..ba4c553 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,10 @@ async fn main() { match rustream::start().await { Ok(_) => { - println!("RuStream session terminated") + println!("\nRuStream session has ended") } Err(err) => { - eprintln!("Error starting rustream: {}", err) + eprintln!("\nError starting RuStream: {}", err) } } } diff --git a/src/routes/media.rs b/src/routes/media.rs index 31a84f8..3aeaa53 100644 --- a/src/routes/media.rs +++ b/src/routes/media.rs @@ -166,7 +166,7 @@ pub async fn stream(config: web::Data>, ("next", &next) ].into_iter().collect::>(); if vec!["jpeg", "jpg", "png", "gif", "tiff", "tif", "bmp", - "svg", "ico", "raw", "psd", "ai", "eps", "pdf"] + "svg", "ico", "raw", "psd", "ai", "eps", "pdf", "webp"] .contains(&render_path.split('.').last() .unwrap() // file extension WILL be present at this point .to_lowercase().as_str()) { diff --git a/src/squire/mod.rs b/src/squire/mod.rs index a330b52..b868fe3 100644 --- a/src/squire/mod.rs +++ b/src/squire/mod.rs @@ -16,3 +16,5 @@ pub mod subtitles; pub mod content; /// Module that handles the authentication and pub mod authenticator; +/// Module that handles parsing command line arguments. +pub mod parser; diff --git a/src/squire/parser.rs b/src/squire/parser.rs new file mode 100644 index 0000000..d38590f --- /dev/null +++ b/src/squire/parser.rs @@ -0,0 +1,51 @@ +use std::env; +use std::process::exit; +use crate::constant::Cargo; + +/// Parses and returns the command-line arguments for RuStream. +/// +/// # Returns +/// +/// A String notion of the `env_file` argument. +pub fn arguments(cargo: &Cargo) -> String { + let args: Vec = env::args().collect(); + + let mut version = false; + let mut env_file = String::new(); + + // Loop through the command-line arguments and parse them. + let mut i = 1; // Start from the second argument (args[0] is the program name). + while i < args.len() { + match args[i].as_str() { + "-h" | "--help" => { + let helper = "RuStream takes the arguments, --env_file and --version/-v\n\n\ + --env_file: Custom filename to load the environment variables. Defaults to '.env'\n\ + --version: Get the package version.\n".to_string(); + println!("Usage: {} [OPTIONS]\n\n{}", args[0], helper); + exit(0) + } + "-V" | "-v" | "--version" => { + version = true; + } + "--env_file" => { + i += 1; // Move to the next argument. + if i < args.len() { + env_file = args[i].clone(); + } else { + println!("--env_file requires a value."); + exit(1) + } + } + _ => { + println!("Unknown argument: {}", args[i]); + exit(1) + } + } + i += 1; + } + if version { + println!("{} {}", &cargo.pkg_name, &cargo.pkg_version); + exit(0) + } + env_file +} diff --git a/src/squire/startup.rs b/src/squire/startup.rs index 5631db6..081fb63 100644 --- a/src/squire/startup.rs +++ b/src/squire/startup.rs @@ -1,4 +1,6 @@ use std; +use crate::constant::Cargo; +use crate::squire; use crate::squire::settings; @@ -234,8 +236,13 @@ fn validate_vars() -> settings::Config { /// # Returns /// /// Converts the config struct into an `Arc` and returns it. -pub fn get_config() -> std::sync::Arc { - let env_file = std::env::var("env_file").unwrap_or(".env".to_string()); +pub fn get_config(cargo: &Cargo) -> std::sync::Arc { + let mut env_file = squire::parser::arguments(cargo); + if env_file.is_empty() { + env_file = std::env::var("env_file") + .unwrap_or(std::env::var("ENV_FILE") + .unwrap_or(".env".to_string())); + } let env_file_path = std::env::current_dir() .unwrap_or_default() .join(env_file);