Add support for environment files
This commit is contained in:
		
							
								
								
									
										7
									
								
								central_backend/Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										7
									
								
								central_backend/Cargo.lock
									
									
									
										generated
									
									
									
								
							@@ -670,6 +670,7 @@ dependencies = [
 | 
				
			|||||||
 "bincode",
 | 
					 "bincode",
 | 
				
			||||||
 "chrono",
 | 
					 "chrono",
 | 
				
			||||||
 "clap",
 | 
					 "clap",
 | 
				
			||||||
 | 
					 "dotenvy",
 | 
				
			||||||
 "env_logger",
 | 
					 "env_logger",
 | 
				
			||||||
 "foreign-types-shared",
 | 
					 "foreign-types-shared",
 | 
				
			||||||
 "fs4",
 | 
					 "fs4",
 | 
				
			||||||
@@ -997,6 +998,12 @@ dependencies = [
 | 
				
			|||||||
 "winapi",
 | 
					 "winapi",
 | 
				
			||||||
]
 | 
					]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[[package]]
 | 
				
			||||||
 | 
					name = "dotenvy"
 | 
				
			||||||
 | 
					version = "0.15.7"
 | 
				
			||||||
 | 
					source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
				
			||||||
 | 
					checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[[package]]
 | 
					[[package]]
 | 
				
			||||||
name = "encode_unicode"
 | 
					name = "encode_unicode"
 | 
				
			||||||
version = "1.0.0"
 | 
					version = "1.0.0"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,7 @@ edition = "2021"
 | 
				
			|||||||
log = "0.4.22"
 | 
					log = "0.4.22"
 | 
				
			||||||
env_logger = "0.11.5"
 | 
					env_logger = "0.11.5"
 | 
				
			||||||
lazy_static = "1.5.0"
 | 
					lazy_static = "1.5.0"
 | 
				
			||||||
 | 
					dotenvy = "0.15.7"
 | 
				
			||||||
clap = { version = "4.5.20", features = ["derive", "env"] }
 | 
					clap = { version = "4.5.20", features = ["derive", "env"] }
 | 
				
			||||||
anyhow = "1.0.89"
 | 
					anyhow = "1.0.89"
 | 
				
			||||||
thiserror = "1.0.64"
 | 
					thiserror = "1.0.64"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -48,6 +48,10 @@ pub enum ConsumptionBackend {
 | 
				
			|||||||
#[derive(Parser, Debug)]
 | 
					#[derive(Parser, Debug)]
 | 
				
			||||||
#[command(version, about, long_about = None)]
 | 
					#[command(version, about, long_about = None)]
 | 
				
			||||||
pub struct AppConfig {
 | 
					pub struct AppConfig {
 | 
				
			||||||
 | 
					    /// Read arguments from env file
 | 
				
			||||||
 | 
					    #[clap(short, long, env)]
 | 
				
			||||||
 | 
					    pub config: Option<String>,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Proxy IP, might end with a star "*"
 | 
					    /// Proxy IP, might end with a star "*"
 | 
				
			||||||
    #[clap(short, long, env)]
 | 
					    #[clap(short, long, env)]
 | 
				
			||||||
    pub proxy_ip: Option<String>,
 | 
					    pub proxy_ip: Option<String>,
 | 
				
			||||||
@@ -114,6 +118,21 @@ lazy_static::lazy_static! {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl AppConfig {
 | 
					impl AppConfig {
 | 
				
			||||||
 | 
					    /// Parse environment variables from file, if requedst
 | 
				
			||||||
 | 
					    pub fn parse_env_file() -> anyhow::Result<()> {
 | 
				
			||||||
 | 
					        if let Some(c) = Self::parse().config {
 | 
				
			||||||
 | 
					            log::info!("Load additional environment variables from {c}");
 | 
				
			||||||
 | 
					            let conf_file = Path::new(&c);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if !conf_file.is_file() {
 | 
				
			||||||
 | 
					                panic!("Specified configuration is not a file!");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            dotenvy::from_path(conf_file)?;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Ok(())
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// Get parsed command line arguments
 | 
					    /// Get parsed command line arguments
 | 
				
			||||||
    pub fn get() -> &'static AppConfig {
 | 
					    pub fn get() -> &'static AppConfig {
 | 
				
			||||||
        &ARGS
 | 
					        &ARGS
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,6 +9,9 @@ use tokio_schedule::{every, Job};
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#[actix_web::main]
 | 
					#[actix_web::main]
 | 
				
			||||||
async fn main() -> std::io::Result<()> {
 | 
					async fn main() -> std::io::Result<()> {
 | 
				
			||||||
 | 
					    // Load additional config from file, if requested
 | 
				
			||||||
 | 
					    AppConfig::parse_env_file().expect("Failed to parse environment file!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Initialize OpenSSL
 | 
					    // Initialize OpenSSL
 | 
				
			||||||
    openssl_sys::init();
 | 
					    openssl_sys::init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user