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