chore: DRY review for schedtasks and use debug > 0 for println, cleanup
This commit is contained in:
		
							parent
							
								
									506a9eb086
								
							
						
					
					
						commit
						72a01648d6
					
				
							
								
								
									
										213
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										213
									
								
								src/main.rs
									
									
									
									
									
								
							@ -11,7 +11,7 @@ use app_env::{
 | 
			
		||||
use clds::monitor::defs::MonitorRules;
 | 
			
		||||
use app_auth::AuthStore;
 | 
			
		||||
use reject_filters::{handle_rejection};
 | 
			
		||||
use anyhow::{Result};
 | 
			
		||||
use anyhow::{anyhow,Result};
 | 
			
		||||
use warp::{
 | 
			
		||||
  // http::{StatusCode},
 | 
			
		||||
  http::{method::Method, HeaderMap},
 | 
			
		||||
@ -36,16 +36,20 @@ pub mod graphql;
 | 
			
		||||
pub mod filters;
 | 
			
		||||
pub mod handlers;
 | 
			
		||||
 | 
			
		||||
async fn create_auth_store(app_env: &AppEnv,verbose: &str) -> AuthStore {
 | 
			
		||||
async fn create_auth_store(app_env: &AppEnv,verbose: isize) -> AuthStore {
 | 
			
		||||
	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)
 | 
			
		||||
  AuthStore::new(&config,AuthStore::create_enforcer(model_path,policy_path).await,verbose)
 | 
			
		||||
}
 | 
			
		||||
async fn up_web_server(webpos: usize) -> Result<()> {
 | 
			
		||||
	let debug = envmnt::get_isize("DEBUG",0);
 | 
			
		||||
	let verbose = envmnt::get_isize("WEB_SERVER_VERBOSE", 0);
 | 
			
		||||
	let mut app_env = AppEnv::default();
 | 
			
		||||
	app_env.curr_web=webpos;
 | 
			
		||||
	println!("Web services: init {} ___________ ", chrono::Utc::now().timestamp());
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
		println!("Web services: init {} ___________ ", chrono::Utc::now().timestamp());
 | 
			
		||||
	}
 | 
			
		||||
	app_env.info = AppInfo::new(
 | 
			
		||||
		"ZTerton",
 | 
			
		||||
		format!("web: {}",&webpos),
 | 
			
		||||
@ -53,41 +57,45 @@ async fn up_web_server(webpos: usize) -> Result<()> {
 | 
			
		||||
		format!("authors: {}",PKG_AUTHORS),
 | 
			
		||||
		format!("{}",PKG_DESCRIPTION),
 | 
			
		||||
	);
 | 
			
		||||
	zterton::init_app(&mut app_env,"").await.unwrap_or_else(|e| 
 | 
			
		||||
	zterton::init_app(&mut app_env,verbose).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 zterton_env != "UNKNOWN" {
 | 
			
		||||
		if verbose != "quiet" {
 | 
			
		||||
		if verbose  > 0 {
 | 
			
		||||
			println!("ZTerton web services at {}",&zterton_env);
 | 
			
		||||
		}
 | 
			
		||||
		return Ok(());
 | 
			
		||||
	}
 | 
			
		||||
	// WEBSERVER.store(1,Ordering::Relaxed);
 | 
			
		||||
  // TODO pass root file-name frmt from AppEnv Config 
 | 
			
		||||
	println!("Loading webserver: {} ({})",&config.name,&app_env.curr_web);
 | 
			
		||||
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
		println!("Loading webserver: {} ({})",&config.name,&app_env.curr_web);
 | 
			
		||||
	}	
 | 
			
		||||
  let (app, socket) = zterton::start_web(&mut app_env).await;
 | 
			
		||||
 | 
			
		||||
  println!("Load app store ...");
 | 
			
		||||
  let app_store = AppStore::new(AppData::new(app_env.to_owned()));
 | 
			
		||||
  
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
 	 println!("Load app store ...");
 | 
			
		||||
	}
 | 
			
		||||
  let app_store = AppStore::new(AppData::new(app_env.to_owned(),verbose));
 | 
			
		||||
  // As static casbin
 | 
			
		||||
  println!("Load auth store ...");
 | 
			
		||||
	let auth_store = create_auth_store(&app_env,"").await;
 | 
			
		||||
 | 
			
		||||
  println!("Load data store ...");
 | 
			
		||||
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
  	println!("Load auth store ...");
 | 
			
		||||
	}
 | 
			
		||||
	let auth_store = create_auth_store(&app_env,verbose).await;
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
  	println!("Load data store ...");
 | 
			
		||||
	}
 | 
			
		||||
  let data_dbs = DataDBs {
 | 
			
		||||
    colls: CollsData::new(app_env.to_owned()),
 | 
			
		||||
    colls: CollsData::new(app_env.to_owned(),verbose),
 | 
			
		||||
    app: app_store.to_owned(),
 | 
			
		||||
    auth: auth_store.to_owned(),
 | 
			
		||||
  };
 | 
			
		||||
  println!("Load web filters ...");
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
 	 println!("Load web filters ...");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
  // let us get some static boxes from config values: 
 | 
			
		||||
  let log_name = app_env.config.st_log_name();
 | 
			
		||||
@ -116,7 +124,7 @@ async fn up_web_server(webpos: usize) -> Result<()> {
 | 
			
		||||
  // If not graphiQL comment/remove next line Interface GiQL MUST BEFORE graphql post with schema
 | 
			
		||||
  // app_api.to_owned()
 | 
			
		||||
  graphql::graphiql(gql_path, giql_path, data_dbs.clone()).await;
 | 
			
		||||
  if giql_path.len() > 0 {
 | 
			
		||||
  if giql_path.len() > 0 && verbose > 0 {
 | 
			
		||||
    println!(
 | 
			
		||||
     "GraphiQL url: {}://{}:{}/{}",
 | 
			
		||||
     &app.protocol, &app.host, &app.port, &giql_path
 | 
			
		||||
@ -151,24 +159,24 @@ async fn up_web_server(webpos: usize) -> Result<()> {
 | 
			
		||||
    &app.protocol, &app.host, &app.port
 | 
			
		||||
  );
 | 
			
		||||
	envmnt::set("ZTERTON", format!("{}:{}",&app.host,&app.port));
 | 
			
		||||
  println!("Web services: done {} __________ ",chrono::Utc::now().timestamp());
 | 
			
		||||
	if debug > 0 {
 | 
			
		||||
 	  println!("Web services: done {} __________ ",chrono::Utc::now().timestamp());
 | 
			
		||||
	}
 | 
			
		||||
	if app.protocol.clone().as_str() == "http" {
 | 
			
		||||
		warp::serve(routes.to_owned())
 | 
			
		||||
			.run(socket)
 | 
			
		||||
			.await;
 | 
			
		||||
	} else {
 | 
			
		||||
		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)
 | 
			
		||||
			.key_path(key_pem)
 | 
			
		||||
			.cert_path(format!("{}/ssl/{}", &config.resources_path, "cert.pem"))
 | 
			
		||||
			.key_path(format!("{}/ssl/{}", &config.resources_path, "key.pem"))
 | 
			
		||||
			.run(socket)
 | 
			
		||||
			.await;
 | 
			
		||||
	}
 | 
			
		||||
	Ok(())
 | 
			
		||||
}
 | 
			
		||||
pub async fn run_cache_clouds() -> Result<()> {
 | 
			
		||||
fn get_args() -> (String,String) {
 | 
			
		||||
	let args: Vec<String> = std::env::args().collect();
 | 
			
		||||
	let mut arg_cfg_path = String::from("");
 | 
			
		||||
	let mut arg_env_path = String::from("");
 | 
			
		||||
@ -179,92 +187,103 @@ pub async fn run_cache_clouds() -> Result<()> {
 | 
			
		||||
			arg_env_path=args[idx+1].to_owned();
 | 
			
		||||
		}
 | 
			
		||||
  });
 | 
			
		||||
  println!("Cache service on Clouds: run {} __________ {}  {} ",chrono::Utc::now().timestamp(),&arg_cfg_path,&arg_env_path);
 | 
			
		||||
	(arg_cfg_path,arg_env_path)
 | 
			
		||||
}
 | 
			
		||||
async fn get_app_env(arg_cfg_path: String,verbose: isize) -> Result<(Cloud,AppEnv)> {
 | 
			
		||||
	let mut cloud = Cloud::default();
 | 
			
		||||
	load_cloud_env(&mut cloud).await;	
 | 
			
		||||
	let mut app_env = AppEnv::default();
 | 
			
		||||
	let config_content = Config::load_file_content("quiet", &arg_cfg_path);
 | 
			
		||||
	let config_content = Config::load_file_content(verbose,&arg_cfg_path);
 | 
			
		||||
  if ! config_content.contains("run_mode") {
 | 
			
		||||
		return Ok(());
 | 
			
		||||
		Err(anyhow!("Run mode not found in config {}", &arg_cfg_path))
 | 
			
		||||
	} else {
 | 
			
		||||
		app_env.config = Config::new(config_content,verbose);
 | 
			
		||||
		Ok((cloud,app_env))
 | 
			
		||||
	}
 | 
			
		||||
	app_env.config = Config::new(config_content,"quiet");
 | 
			
		||||
  let app_store = AppStore::new(AppData::new(app_env.to_owned()));
 | 
			
		||||
  let auth_store = create_auth_store(&app_env,"quiet").await;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
async fn set_reqenv(app_env: &AppEnv,verbose: isize) -> ReqEnv {
 | 
			
		||||
	let app_store =	AppStore::new(AppData::new(app_env.to_owned(),verbose));
 | 
			
		||||
	let auth_store = create_auth_store(&app_env,verbose).await;
 | 
			
		||||
	let mut headers = HeaderMap::new();
 | 
			
		||||
  headers.insert(http::header::HOST, "localhost".parse().unwrap());
 | 
			
		||||
	let reqenv = ReqEnv::new( 
 | 
			
		||||
		app_store, auth_store,
 | 
			
		||||
	ReqEnv::new( 
 | 
			
		||||
		app_store, 
 | 
			
		||||
		auth_store,
 | 
			
		||||
		headers,
 | 
			
		||||
	  Method::GET,
 | 
			
		||||
		"/config", "config", "kloud"
 | 
			
		||||
	);
 | 
			
		||||
	)
 | 
			
		||||
}
 | 
			
		||||
pub async fn run_cache_clouds() -> Result<()> {
 | 
			
		||||
	let (arg_cfg_path,arg_env_path) = get_args();
 | 
			
		||||
	let now = chrono::Utc::now().timestamp();
 | 
			
		||||
	let verbose = envmnt::get_isize("DEBUG", 0);
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
  	println!("Cache service on Clouds: run {} __________ {}  {} ",&now,&arg_cfg_path,&arg_env_path);
 | 
			
		||||
	}
 | 
			
		||||
	let (cloud, app_env) = match get_app_env(arg_cfg_path,verbose).await {
 | 
			
		||||
		Ok((c,e)) => (c,e),
 | 
			
		||||
		Err(e) => {
 | 
			
		||||
      println!("Cache service on Clouds: done {} __________ ",&now);
 | 
			
		||||
			return Err(e);
 | 
			
		||||
		},
 | 
			
		||||
	};
 | 
			
		||||
	let reqenv = set_reqenv(&app_env,verbose).await;
 | 
			
		||||
	let res = make_cloud_cache(&reqenv,&cloud).await;
 | 
			
		||||
  println!("Cache service on Clouds: done {} __________ ",chrono::Utc::now().timestamp());
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
  	println!("Cache service on Clouds: done {} __________ ",&now);
 | 
			
		||||
	}
 | 
			
		||||
	res
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub async fn run_check_clouds() -> Result<()> {
 | 
			
		||||
	let args: Vec<String> = std::env::args().collect();
 | 
			
		||||
	let mut arg_cfg_path = String::from("");
 | 
			
		||||
	let mut arg_env_path = String::from("");
 | 
			
		||||
	args.iter().enumerate().for_each(|(idx,arg)| {
 | 
			
		||||
		if arg == "-c" { 
 | 
			
		||||
			arg_cfg_path=args[idx+1].to_owned();
 | 
			
		||||
		} else if arg == "-e" { 
 | 
			
		||||
			arg_env_path=args[idx+1].to_owned();
 | 
			
		||||
		}
 | 
			
		||||
  });
 | 
			
		||||
  println!("Check Cloud services: run {} __________ {} {} ",chrono::Utc::now().timestamp(),&arg_cfg_path,&arg_env_path);
 | 
			
		||||
	let mut cloud = Cloud::default();
 | 
			
		||||
	load_cloud_env(&mut cloud).await;	
 | 
			
		||||
	let mut app_env = AppEnv::default();
 | 
			
		||||
	let config_content = Config::load_file_content("quiet",&arg_cfg_path);
 | 
			
		||||
  if ! config_content.contains("run_mode") {
 | 
			
		||||
		return Ok(());
 | 
			
		||||
	let (arg_cfg_path,arg_env_path) = get_args();
 | 
			
		||||
	let now = chrono::Utc::now().timestamp();
 | 
			
		||||
	let verbose = envmnt::get_isize("DEBUG", 0);
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
 	  println!("Check Cloud services: run {} __________ {} {} ",&now,&arg_cfg_path,&arg_env_path);
 | 
			
		||||
	}
 | 
			
		||||
	app_env.config = Config::new(config_content,"quiet");
 | 
			
		||||
  let app_store = AppStore::new(AppData::new(app_env.to_owned()));
 | 
			
		||||
  let auth_store = create_auth_store(&app_env,"quiet").await;
 | 
			
		||||
 | 
			
		||||
	let mut headers = HeaderMap::new();
 | 
			
		||||
  headers.insert(http::header::HOST, "localhost".parse().unwrap());
 | 
			
		||||
	let reqenv = ReqEnv::new( 
 | 
			
		||||
		app_store, auth_store,
 | 
			
		||||
		headers,
 | 
			
		||||
	  Method::GET,
 | 
			
		||||
		"/config", "config", "kloud"
 | 
			
		||||
	);
 | 
			
		||||
	let (cloud, app_env) = match get_app_env(arg_cfg_path,verbose).await {
 | 
			
		||||
		Ok((c,e)) => (c,e),
 | 
			
		||||
		Err(e) => {
 | 
			
		||||
  	  println!("Check Cloud service: done {} __________ ",&now);
 | 
			
		||||
			return Err(e);
 | 
			
		||||
		},
 | 
			
		||||
	};
 | 
			
		||||
	let reqenv = set_reqenv(&app_env,verbose).await;
 | 
			
		||||
	let res = run_clouds_check(&reqenv,&cloud).await;
 | 
			
		||||
  println!("Check Cloud service: done {} __________ ",chrono::Utc::now().timestamp());
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
 	   println!("Check Cloud service: done {} __________ ",&now);
 | 
			
		||||
	}
 | 
			
		||||
	res
 | 
			
		||||
}
 | 
			
		||||
pub async fn run_clouds_monitor() -> Result<()> {
 | 
			
		||||
	let args: Vec<String> = std::env::args().collect();
 | 
			
		||||
	let mut arg_cfg_path = String::from("");
 | 
			
		||||
	let mut arg_env_path = String::from("");
 | 
			
		||||
	args.iter().enumerate().for_each(|(idx,arg)| {
 | 
			
		||||
		if arg == "-c" { 
 | 
			
		||||
			arg_cfg_path=args[idx+1].to_owned();
 | 
			
		||||
		} else if arg == "-e" { 
 | 
			
		||||
			arg_env_path=args[idx+1].to_owned();
 | 
			
		||||
		}
 | 
			
		||||
  });
 | 
			
		||||
  println!("Monitor Cloud: run {} __________ {}  {} ",chrono::Utc::now().timestamp(),&arg_cfg_path,&arg_env_path);
 | 
			
		||||
	let mut cloud = Cloud::default();
 | 
			
		||||
	load_cloud_env(&mut cloud).await;	
 | 
			
		||||
	let mut app_env = AppEnv::default();
 | 
			
		||||
	let config_content = Config::load_file_content("quiet",&arg_cfg_path);
 | 
			
		||||
  if ! config_content.contains("run_mode") {
 | 
			
		||||
	let (arg_cfg_path,arg_env_path) = get_args();
 | 
			
		||||
	let now = chrono::Utc::now().timestamp();
 | 
			
		||||
	let verbose = envmnt::get_isize("DEBUG", 0);
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
  	println!("Monitor Cloud: run {} __________ {}  {} ",&now,arg_cfg_path,&arg_env_path);
 | 
			
		||||
	}
 | 
			
		||||
	let (cloud, app_env) = match get_app_env(arg_cfg_path,verbose).await {
 | 
			
		||||
		Ok((c,e)) => (c,e),
 | 
			
		||||
		Err(e) => {
 | 
			
		||||
  		println!("Monitor Cloud done {} __________ ",&now);
 | 
			
		||||
			return Err(e);
 | 
			
		||||
		},
 | 
			
		||||
	};	
 | 
			
		||||
	let monitor_rules = MonitorRules::load(
 | 
			
		||||
		&app_env.config.monitor_rules_path,
 | 
			
		||||
		&app_env.config.monitor_rules_file,
 | 
			
		||||
		&app_env.config.monitor_rules_format
 | 
			
		||||
	);
 | 
			
		||||
	if monitor_rules.rules.len() == 0 {
 | 
			
		||||
		eprintln!("No monitor rules found");
 | 
			
		||||
		return Ok(());
 | 
			
		||||
	} 
 | 
			
		||||
	app_env.config = Config::new(config_content,"quiet");
 | 
			
		||||
	//let monitor_sched_task = app_env.config.get_schedtask("monitor");
 | 
			
		||||
	let monitor_rules = MonitorRules::load(&app_env.config.monitor_rules_path,&app_env.config.monitor_rules_file,&app_env.config.monitor_rules_format);
 | 
			
		||||
	if monitor_rules.rules.len() > 0 {
 | 
			
		||||
		monitor_rules.run(cloud,app_env).await?
 | 
			
		||||
	let res = monitor_rules.run(cloud,app_env).await;
 | 
			
		||||
	if verbose > 0 {
 | 
			
		||||
 	 println!("Monitor Cloud done {} __________ ",&now);
 | 
			
		||||
	}
 | 
			
		||||
	Ok(())
 | 
			
		||||
	res
 | 
			
		||||
}
 | 
			
		||||
#[tokio::main] 
 | 
			
		||||
pub async fn main() -> BxDynResult<()> {  //std::io::Result<()> {  
 | 
			
		||||
@ -280,6 +299,7 @@ pub async fn main() -> BxDynResult<()> {  //std::io::Result<()> {
 | 
			
		||||
			_ => println!("{}",PKG_NAME),
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	let debug=envmnt::get_isize("DEBUG",0);
 | 
			
		||||
	let mut sched = JobScheduler::new();
 | 
			
		||||
	let mut arg_cfg_path = String::from("");
 | 
			
		||||
	let mut arg_env_path = String::from("");
 | 
			
		||||
@ -295,11 +315,11 @@ pub async fn main() -> BxDynResult<()> {  //std::io::Result<()> {
 | 
			
		||||
		dotenv::from_path(env_path)?;
 | 
			
		||||
	}
 | 
			
		||||
	pretty_env_logger::init();
 | 
			
		||||
	let config_content = Config::load_file_content("quiet", &arg_cfg_path);
 | 
			
		||||
	let config_content = Config::load_file_content(debug, &arg_cfg_path);
 | 
			
		||||
	if !config_content.contains("run_mode") {
 | 
			
		||||
		 panic!("Error no run_mode found or config path incomplete");
 | 
			
		||||
		panic!("Error no run_mode found or config path incomplete");
 | 
			
		||||
	}
 | 
			
		||||
	let config = Config::new(config_content,"quiet");
 | 
			
		||||
	let config = Config::new(config_content,debug);
 | 
			
		||||
	if config.run_schedtasks {
 | 
			
		||||
		for it in &config.schedtasks {
 | 
			
		||||
			if ! it.on_start {
 | 
			
		||||
@ -318,7 +338,9 @@ pub async fn main() -> BxDynResult<()> {  //std::io::Result<()> {
 | 
			
		||||
	}
 | 
			
		||||
	if config.run_websrvrs {
 | 
			
		||||
		for (pos,it) in config.websrvrs.iter().enumerate() {
 | 
			
		||||
			println!("{} -> {}",it.name,pos);
 | 
			
		||||
			if debug > 1 {
 | 
			
		||||
				println!("{} -> {}",it.name,pos);
 | 
			
		||||
			}
 | 
			
		||||
			tokio::spawn(async move {up_web_server(pos).await});
 | 
			
		||||
			tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
 | 
			
		||||
		}
 | 
			
		||||
@ -329,7 +351,6 @@ pub async fn main() -> BxDynResult<()> {  //std::io::Result<()> {
 | 
			
		||||
				eprintln!("Task {} no schedule defined",&it.name);
 | 
			
		||||
				continue;
 | 
			
		||||
			}
 | 
			
		||||
		  let debug=envmnt::get_isize("DEBUG",0);
 | 
			
		||||
			let res = match it.name.as_str() {
 | 
			
		||||
				"monitor" => 
 | 
			
		||||
					sched.add(Job::new(&it.schedule.to_owned(), move |uuid, _l| {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user