chore: home handle via json request

This commit is contained in:
Jesús Pérez Lorenzo 2021-10-12 11:13:51 +01:00
parent 56695fc2dd
commit 5e36269671

149
src/handlers/h_home.rs Normal file
View File

@ -0,0 +1,149 @@
use std::collections::{HashMap,BTreeMap};
use warp::{
// http::{StatusCode},
http::{method::Method, HeaderMap, HeaderValue},
Reply, Rejection,
};
use reqenv::ReqEnv;
// use app_env::profile::Profile;
use app_auth::{UserCtx};
// use kloud::{
// defs::{
// KloudQueryConfigFilters,
// },
// };
use clds::defs::{KloudCheckHome};
use clds::clouds::defs::{Cloud};
use crate::defs::{DataDBs}; // ,CollsData};
use clds::clouds::on_clouds::{get_cloud_check,get_apps_check};
pub async fn get_tsks_apps_check(reqenv: &ReqEnv, cld_indx: usize, _auth: UserCtx,_role: &str) -> String {
let result_apps = get_apps_check(&reqenv).await;
// println!("Result apps: {}",&result_apps);
let res_apps: Vec<KloudCheckHome> = serde_json::from_str(&result_apps).unwrap_or_else(|e| {
println!("Error serde apps json: {}",e);
Vec::new()
});
// let mut list_groups = String::from("");
let mut grp_apps_hash: HashMap<String, BTreeMap<String,String>> = HashMap::new();
if let Some(kld) = res_apps.get(cld_indx) {
for grp in &kld.groups {
// if !list_groups.is_empty() {
// list_groups.push(',');
// }
// list_groups.push_str(&grp.name);
let mut m_items = BTreeMap::new();
for itm in &grp.items {
if let Some(data_liveness) = &itm.liveness {
m_items.insert(format!("{}",&itm.name),data_liveness.to_string());
}
}
grp_apps_hash.insert(format!("{}",grp.name),m_items);
}
}
let result_cloud = get_cloud_check(&reqenv).await;
// println!("Result cloud: {}",&result_cloud);
let res_cloud: Vec<KloudCheckHome> = serde_json::from_str(&result_cloud).unwrap_or_else(|e| {
println!("Error serde apps json: {}",e);
Vec::new()
});
let mut grp_cloud_hash: HashMap<String, BTreeMap<String,String>> = HashMap::new();
if let Some(kld) = res_cloud.get(cld_indx) {
for grp in &kld.groups {
let mut m_items = BTreeMap::new();
for itm in &grp.items {
if let Some(data_liveness) = &itm.liveness {
m_items.insert(format!("{}",&itm.name),data_liveness.to_string());
}
}
grp_cloud_hash.insert(format!("{}",grp.name),m_items);
}
}
// ctx.insert("groups_cloud".to_string(),&grp_cloud_hash);
let res_apps_json=serde_json::to_string(&grp_apps_hash).unwrap_or_else(|e|{
println!("Error serde from value: {}",e);
String::from("")
});
let res_cloud_json=serde_json::to_string(&grp_cloud_hash).unwrap_or_else(|e|{
println!("Error serde from value: {}",e);
String::from("")
});
let result = format!("{}:{}{}:{}{}",r#"{"group_apps""#,&res_apps_json,r#","group_cloud""#,&res_cloud_json,r#"}"#);
result.to_owned()
}
pub async fn home(
header: HeaderMap<HeaderValue>,
method: Method,
// user: UserCtx,
db: DataDBs,
_cloud: Cloud,
prfx: String,
// headers: warp::http::HeaderMap
// path: warp::path::FullPath, headers: warp::http::HeaderMap
) -> Result<impl Reply, Rejection> {
let reqenv = ReqEnv::new(db.app, db.auth, header, method, "/home", "home", &prfx);
// let allow_origin = reqenv.websrvr().allow_origin;
// let opts = KloudQueryConfigFilters::default();
// let source = format!("{}/{}","ma","");
match reqenv.user_authentication().await {
Ok(auth) => {
// dbg!("auth: {}",&auth);
// println!("User: {} | {}",&user.user_id,&user.token);
// if let Some(lang) = reqtasks.params().get("lang") {
// res.render_json_text(&get_lang_items_str("langs",req_lang,"yaml"))
// } else {
// res.render_json_text(&get_lang_items_str("langs",&reqtasks.lang(),"yaml"))
// }
// log::debug!("LANG: {} - {}",language, lang);
// dbg!("LANG: {} - {}",language, lang);
let role = reqenv.req.user_role().await;
// reqenv.is_admin()
let result = get_tsks_apps_check(&reqenv,1,auth,&role).await;
// let mut path = format!("{}/profiles/{}/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx,&auth.user_id);
// if ! std::path::Path::new(&path).exists() {
// path = format!("{}/profiles/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx);
// }
// let content = Profile::load_fs_content(path.into());
// let lang = opts.lang.unwrap_or_else(|| String::from("es"));
// let section = opts.section.unwrap_or_else(|| String::from(""));
// let lang_items = LangItems::new("langs/ta",&lang,"yaml");
// let result = lang_items.get_items_str(&section);
// let res = Profile::to_yaml(content); // String::from("");
// let result = serde_json::to_string(&res).unwrap_or_else(|_| String::from(""));
Ok(warp::http::Response::builder()
.body(result.to_string())
.into_response())
/*
Ok(warp::reply::with_header(
warp::http::Response::new(result),
"Access-Control-Allow-Origin",
&allow_origin))
*/
// warp::reply::json(&res),
// Ok(warp::reply::with_header(
// warp::http::Response::new(result),
// "Access-Control-Allow-Origin",
// &allow_origin))
// Ok(warp::reply::json(&result))
},
Err(e) => {
// let result = format!("Error: no credentials found");
// println!("{}",e);
let result = get_tsks_apps_check(&reqenv,1,UserCtx::default(),"").await;
Ok(warp::http::Response::builder()
.body(result.to_string())
.into_response())
/*
Ok(warp::reply::with_header(
warp::http::Response::new(result),
"Access-Control-Allow-Origin",
&allow_origin))
*/
}
}
}