Skip to content

Commit

Permalink
Allow passing env_file as an argument
Browse files Browse the repository at this point in the history
Add more file extensions for images
  • Loading branch information
dormant-user committed Feb 25, 2024
1 parent 3b44e75 commit 9949dc7
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion src/routes/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub async fn stream(config: web::Data<Arc<squire::settings::Config>>,
("next", &next)
].into_iter().collect::<HashMap<_, _>>();
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()) {
Expand Down
2 changes: 2 additions & 0 deletions src/squire/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
51 changes: 51 additions & 0 deletions src/squire/parser.rs
Original file line number Diff line number Diff line change
@@ -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<String> = 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
}
11 changes: 9 additions & 2 deletions src/squire/startup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std;
use crate::constant::Cargo;
use crate::squire;

use crate::squire::settings;

Expand Down Expand Up @@ -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<settings::Config> {
let env_file = std::env::var("env_file").unwrap_or(".env".to_string());
pub fn get_config(cargo: &Cargo) -> std::sync::Arc<settings::Config> {
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);
Expand Down

0 comments on commit 9949dc7

Please sign in to comment.