From 34d1e08b15ca2c5a9b578c90ec49922a15822384 Mon Sep 17 00:00:00 2001 From: Pol Espinasa Date: Sun, 21 Apr 2024 20:16:40 +0200 Subject: [PATCH] Multiple path config file --- server/grouphug-server/src/main.rs | 57 ++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/server/grouphug-server/src/main.rs b/server/grouphug-server/src/main.rs index e81b3c6..675548d 100644 --- a/server/grouphug-server/src/main.rs +++ b/server/grouphug-server/src/main.rs @@ -3,32 +3,60 @@ mod utils; mod config; mod server; use crate::utils::transactions::validate_tx_query_one_to_one_single_anyone_can_pay; -use std::fs; use once_cell::sync::Lazy; use crate::config::Config; +use crate::server::group::Group; -pub static CONFIG: Lazy = Lazy::new(|| { - let contents = fs::read_to_string("Config.toml") - .expect("Something went wrong reading the file"); - let config: Config = toml::from_str(&contents) - .expect("Unable to parse the toml file"); - - config -}); - -use crate::server::group::Group; // External libraries use std::thread; use std::io::{Read, Write}; use std::net::{TcpListener, TcpStream}; use std::str; +use std::fs; use std::env; use std::sync::{Arc, Mutex}; use hex::decode as hex_decode; use bdk::bitcoin::{Transaction,consensus::encode::deserialize}; +pub static CONFIG: Lazy = Lazy::new(|| { + // Obtenir els arguments de la línia de comandes + let args: Vec = env::args().collect(); + + // Especificar el camí per defecte del fitxer Config.toml + let default_path = "Config.toml"; + + // Utilitzar l'argument proporcionat si existeix, sinó utilitzar el camí per defecte + let config_path = if args.len() > 1 { + &args[1] + } else { + default_path + }; + + let contents = fs::read_to_string(config_path) + .expect("Something went wrong reading the file"); + + let config: Config = toml::from_str(&contents) + .expect("Unable to parse the toml file"); + + config +}); + +/* +pub static CONFIG: Lazy = Lazy::new(|| { + let contents = fs::read_to_string("Config.toml") + .expect("Something went wrong reading the file"); + + let config: Config = toml::from_str(&contents) + .expect("Unable to parse the toml file"); + + config +}); +*/ + + + // Array with Group list type GroupHug = Group; static GLOBAL_GROUPS: Lazy>>> = Lazy::new(|| Arc::new(Mutex::new(Vec::new()))); @@ -189,10 +217,11 @@ fn handle_client(mut stream: TcpStream) { fn main() { + let args: Vec = env::args().collect(); - if args.len() != 1 { - eprintln!("No arguments accepted"); - } + //if args.len() != 1 { + // eprintln!("No arguments accepted"); + //} // Fromat endpoint data from config file let endpoint: String = format!("{}:{}", &crate::CONFIG.server.ip, &crate::CONFIG.server.port);