From 54e67f2c923e206e4ce5753f0704248ab4bfcc3e Mon Sep 17 00:00:00 2001 From: JesusPerez Date: Tue, 7 Sep 2021 15:01:04 +0100 Subject: [PATCH] chore: WebServer in config as vector for Multiples Webs mode --- src/main.rs | 80 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index d2ad71b..d9d2f9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use app_env::{ appenv::AppEnv, appinfo::AppInfo, appdata::AppData, - config::Config, + config::{Config,WebServer} }; use app_auth::AuthStore; use reject_filters::{handle_rejection}; @@ -53,15 +53,30 @@ pub mod handlers; // pub const POLICY_PATH: &String = String::from("./auth/policy.csv"); async fn create_auth_store(app_env: &AppEnv,verbose: &str) -> AuthStore { - let model_path = app_env.config.st_auth_model_path(); - let policy_path = app_env.config.st_auth_policy_path(); - AuthStore::new(&app_env.config,AuthStore::create_enforcer(model_path,policy_path).await,&verbose) + let config = app_env.get_curr_websrvr_config(); + let model_path = config.st_auth_model_path(); + let policy_path = config.st_auth_policy_path(); + AuthStore::new(&config,AuthStore::create_enforcer(model_path,policy_path).await,&verbose) } -async fn up_web_server() -> Result<()> { - let webserver_status = WEBSERVER.load(Ordering::Relaxed); - let zterton_env = envmnt::get_or("ZTERTON", "UNKNOWN"); +async fn up_web_server(webpos: usize) -> Result<()> { + let mut app_env = AppEnv::default(); + app_env.curr_web=webpos; + println!("Web services: init {} ___________ ", chrono::Utc::now().timestamp()); + app_env.info = AppInfo::new( + "ZTerton", + format!("web: {}",&webpos), + format!("version: {}",PKG_VERSION), + format!("Authors: {}",PKG_AUTHORS), + ).await; + zterton::init_app(&mut app_env,"").await.unwrap_or_else(|e| + panic!("Error loadding app environment {}",e) + ); + let config = app_env.get_curr_websrvr_config(); + // let webserver_status = WEBSERVER.load(Ordering::Relaxed); + let zterton_env = envmnt::get_or(format!("ZTERTON_{}",&config.name).as_str(), "UNKNOWN"); let verbose = envmnt::get_or("WEB_SERVER_VERBOSE", ""); - if webserver_status != 0 { + // if webserver_status != 0 { + if zterton_env != "UNKNOWN" { if verbose != "quiet" { println!("ZTerton web services at {}",&zterton_env); } @@ -85,18 +100,12 @@ async fn up_web_server() -> Result<()> { } */ } - WEBSERVER.store(1,Ordering::Relaxed); - let mut app_env = AppEnv::default(); - app_env.info = AppInfo::new( - "Zterton", - format!("version: {}",PKG_VERSION), - format!("Authors: {}",PKG_AUTHORS), - ).await; - println!("Web services: init {} ___________ ", chrono::Utc::now().timestamp()); - zterton::init_app(&mut app_env,"").await?; + // WEBSERVER.store(1,Ordering::Relaxed); + // TODO pass root file-name frmt from AppEnv Config // if init at load // set_ta_data(&app_env).await?; + println!("Loading webserver: {} ({})",&config.name,&app_env.curr_web); let (app, socket) = zterton::start_web(&mut app_env).await; @@ -123,13 +132,13 @@ async fn up_web_server() -> Result<()> { // let us get some static boxes from config values: let log_name = app_env.config.st_log_name(); // Path for static files - let html_path = app_env.config.st_html_path(); + let html_path = config.st_html_path(); // If not graphQL comment/remove next line - let gql_path = app_env.config.st_gql_req_path(); + let gql_path = config.st_gql_req_path(); // If not graphiQL comment/remove next line Interface GiQL - let giql_path = app_env.config.st_giql_req_path(); + let giql_path = config.st_giql_req_path(); - let origins: Vec<&str> = app_env.config.allow_origin.iter().map(AsRef::as_ref).collect(); + let origins: Vec<&str> = config.allow_origin.iter().map(AsRef::as_ref).collect(); let cors = warp::cors() //.allow_any_origin() .allow_origins(origins) @@ -164,12 +173,12 @@ async fn up_web_server() -> Result<()> { .filters_config(data_dbs.clone(),cloud.clone(),cors.clone()); // let ta_api = - // filters::CollFilters::new("ta").filters(&app_env.config, data_dbs.clone(),cors.clone()); + // filters::CollFilters::new("ta").filters(&config, data_dbs.clone(),cors.clone()); -// let tp_api = filters::CollFilters::new("tp").filters(&app_env.config, data_dbs.clone(),cors.clone()); -// .or(tracking_point::load_tp_filters().filters(&app_env.config, data_dbs.clone())) -// .or(topographic_anatomy::filters::ta(&app_env.config, data_dbs.clone())) -// .or(tracking_point::filters::tp(&app_env.config, data_dbs.clone())) +// let tp_api = filters::CollFilters::new("tp").filters(&config, data_dbs.clone(),cors.clone()); +// .or(tracking_point::load_tp_filters().filters(&config, data_dbs.clone())) +// .or(topographic_anatomy::filters::ta(&config, data_dbs.clone())) +// .or(tracking_point::filters::tp(&config, data_dbs.clone())) let file_api = app_file_filters::files(app_store.clone(),auth_store.clone()).with(cors.clone()); // Path for static files, better to be LAST @@ -199,8 +208,8 @@ async fn up_web_server() -> Result<()> { .run(socket) .await; } else { - let cert_pem = format!("{}/ssl/{}", app_env.config.resources_path, "cert.pem"); - let key_pem = format!("{}/ssl/{}", &app_env.config.resources_path, "key.pem"); + let cert_pem = format!("{}/ssl/{}", &config.resources_path, "cert.pem"); + let key_pem = format!("{}/ssl/{}", &config.resources_path, "key.pem"); warp::serve(routes) .tls() .cert_path(cert_pem) @@ -315,6 +324,7 @@ pub fn main() -> Result<()> { let loop_duration: u64; let run_cache: bool; let run_check: bool; + let websrvrs: Vec; { let config_content = Config::load_file_content("quiet", &arg_cfg_path); if config_content.contains("run_mode") { @@ -323,6 +333,7 @@ pub fn main() -> Result<()> { // loop_duration = 10; run_cache = config.run_cache; run_check = config.run_check; + websrvrs = config.websrvrs; if run_cache { println!("Running 'cloud_cache' every {} seconds in LOOP",&loop_duration); } @@ -333,8 +344,10 @@ pub fn main() -> Result<()> { loop_duration = 0; run_cache = false; run_check = false; + websrvrs = Vec::new(); } } + // println!("content: {}",&config_content); let rt = tokio::runtime::Runtime::new().unwrap_or_else(|e| panic!("Error create tokio runtime {}",e) @@ -342,10 +355,13 @@ pub fn main() -> Result<()> { let webs_rt = tokio::runtime::Runtime::new().unwrap_or_else(|e| panic!("Error create webs tokio runtime {}",e) ); - let mut web_tasks = Vec::new(); - webs_rt.block_on(async move { - web_tasks.push(tokio::spawn(async move {up_web_server().await })); - tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; + // let mut web_tasks = Vec::new(); + websrvrs.iter().enumerate().for_each(|(pos,it)| { + webs_rt.block_on(async move { + println!("{} -> {}",it.name,pos); + tokio::spawn(async move {up_web_server(pos).await}); + tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; + }); }); loop { rt.block_on(async move {