chore: WebServer in config as vector for Multiples Webs mode

This commit is contained in:
Jesús Pérez Lorenzo 2021-09-07 15:01:04 +01:00
parent c5a3c470ac
commit 54e67f2c92

View File

@ -4,7 +4,7 @@ use app_env::{
appenv::AppEnv, appenv::AppEnv,
appinfo::AppInfo, appinfo::AppInfo,
appdata::AppData, appdata::AppData,
config::Config, config::{Config,WebServer}
}; };
use app_auth::AuthStore; use app_auth::AuthStore;
use reject_filters::{handle_rejection}; use reject_filters::{handle_rejection};
@ -53,15 +53,30 @@ pub mod handlers;
// pub const POLICY_PATH: &String = String::from("./auth/policy.csv"); // pub const POLICY_PATH: &String = String::from("./auth/policy.csv");
async fn create_auth_store(app_env: &AppEnv,verbose: &str) -> AuthStore { async fn create_auth_store(app_env: &AppEnv,verbose: &str) -> AuthStore {
let model_path = app_env.config.st_auth_model_path(); let config = app_env.get_curr_websrvr_config();
let policy_path = app_env.config.st_auth_policy_path(); let model_path = config.st_auth_model_path();
AuthStore::new(&app_env.config,AuthStore::create_enforcer(model_path,policy_path).await,&verbose) 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<()> { async fn up_web_server(webpos: usize) -> Result<()> {
let webserver_status = WEBSERVER.load(Ordering::Relaxed); let mut app_env = AppEnv::default();
let zterton_env = envmnt::get_or("ZTERTON", "UNKNOWN"); 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", ""); let verbose = envmnt::get_or("WEB_SERVER_VERBOSE", "");
if webserver_status != 0 { // if webserver_status != 0 {
if zterton_env != "UNKNOWN" {
if verbose != "quiet" { if verbose != "quiet" {
println!("ZTerton web services at {}",&zterton_env); println!("ZTerton web services at {}",&zterton_env);
} }
@ -85,18 +100,12 @@ async fn up_web_server() -> Result<()> {
} }
*/ */
} }
WEBSERVER.store(1,Ordering::Relaxed); // 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?;
// TODO pass root file-name frmt from AppEnv Config // TODO pass root file-name frmt from AppEnv Config
// if init at load // if init at load
// set_ta_data(&app_env).await?; // 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; 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 us get some static boxes from config values:
let log_name = app_env.config.st_log_name(); let log_name = app_env.config.st_log_name();
// Path for static files // 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 // 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 // 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() let cors = warp::cors()
//.allow_any_origin() //.allow_any_origin()
.allow_origins(origins) .allow_origins(origins)
@ -164,12 +173,12 @@ async fn up_web_server() -> Result<()> {
.filters_config(data_dbs.clone(),cloud.clone(),cors.clone()); .filters_config(data_dbs.clone(),cloud.clone(),cors.clone());
// let ta_api = // 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()); // let tp_api = filters::CollFilters::new("tp").filters(&config, data_dbs.clone(),cors.clone());
// .or(tracking_point::load_tp_filters().filters(&app_env.config, data_dbs.clone())) // .or(tracking_point::load_tp_filters().filters(&config, data_dbs.clone()))
// .or(topographic_anatomy::filters::ta(&app_env.config, data_dbs.clone())) // .or(topographic_anatomy::filters::ta(&config, data_dbs.clone()))
// .or(tracking_point::filters::tp(&app_env.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()); let file_api = app_file_filters::files(app_store.clone(),auth_store.clone()).with(cors.clone());
// Path for static files, better to be LAST // Path for static files, better to be LAST
@ -199,8 +208,8 @@ async fn up_web_server() -> Result<()> {
.run(socket) .run(socket)
.await; .await;
} else { } else {
let cert_pem = format!("{}/ssl/{}", app_env.config.resources_path, "cert.pem"); let cert_pem = format!("{}/ssl/{}", &config.resources_path, "cert.pem");
let key_pem = format!("{}/ssl/{}", &app_env.config.resources_path, "key.pem"); let key_pem = format!("{}/ssl/{}", &config.resources_path, "key.pem");
warp::serve(routes) warp::serve(routes)
.tls() .tls()
.cert_path(cert_pem) .cert_path(cert_pem)
@ -315,6 +324,7 @@ pub fn main() -> Result<()> {
let loop_duration: u64; let loop_duration: u64;
let run_cache: bool; let run_cache: bool;
let run_check: bool; let run_check: bool;
let websrvrs: Vec<WebServer>;
{ {
let config_content = Config::load_file_content("quiet", &arg_cfg_path); let config_content = Config::load_file_content("quiet", &arg_cfg_path);
if config_content.contains("run_mode") { if config_content.contains("run_mode") {
@ -323,6 +333,7 @@ pub fn main() -> Result<()> {
// loop_duration = 10; // loop_duration = 10;
run_cache = config.run_cache; run_cache = config.run_cache;
run_check = config.run_check; run_check = config.run_check;
websrvrs = config.websrvrs;
if run_cache { if run_cache {
println!("Running 'cloud_cache' every {} seconds in LOOP",&loop_duration); println!("Running 'cloud_cache' every {} seconds in LOOP",&loop_duration);
} }
@ -333,8 +344,10 @@ pub fn main() -> Result<()> {
loop_duration = 0; loop_duration = 0;
run_cache = false; run_cache = false;
run_check = false; run_check = false;
websrvrs = Vec::new();
} }
} }
// println!("content: {}",&config_content); // println!("content: {}",&config_content);
let rt = tokio::runtime::Runtime::new().unwrap_or_else(|e| let rt = tokio::runtime::Runtime::new().unwrap_or_else(|e|
panic!("Error create tokio runtime {}",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| let webs_rt = tokio::runtime::Runtime::new().unwrap_or_else(|e|
panic!("Error create webs tokio runtime {}",e) panic!("Error create webs tokio runtime {}",e)
); );
let mut web_tasks = Vec::new(); // let mut web_tasks = Vec::new();
webs_rt.block_on(async move { websrvrs.iter().enumerate().for_each(|(pos,it)| {
web_tasks.push(tokio::spawn(async move {up_web_server().await })); webs_rt.block_on(async move {
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; println!("{} -> {}",it.name,pos);
tokio::spawn(async move {up_web_server(pos).await});
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
});
}); });
loop { loop {
rt.block_on(async move { rt.block_on(async move {