Skip to content

Commit

Permalink
Multiple path config file
Browse files Browse the repository at this point in the history
  • Loading branch information
polespinasa committed Apr 21, 2024
1 parent efca378 commit 34d1e08
Showing 1 changed file with 43 additions and 14 deletions.
57 changes: 43 additions & 14 deletions server/grouphug-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config> = 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<Config> = Lazy::new(|| {
// Obtenir els arguments de la línia de comandes
let args: Vec<String> = 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<Config> = 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<Arc<Mutex<Vec<GroupHug>>>> = Lazy::new(|| Arc::new(Mutex::new(Vec::new())));
Expand Down Expand Up @@ -189,10 +217,11 @@ fn handle_client(mut stream: TcpStream) {

fn main() {


let args: Vec<String> = 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);
Expand Down

0 comments on commit 34d1e08

Please sign in to comment.