lib_defs/app_env/src/appenv.rs

95 lines
2.0 KiB
Rust

//
// Copyright 2021, Jesús Pérez Lorenzo
//
use std::collections::HashMap;
use serde::Deserialize;
use crate::{
config::{Config,WebServer},
module::Module,
appinfo::AppInfo,
};
#[derive(Clone, Debug, Default, Deserialize)]
pub struct DataCollUsers {
pub fields: String,
}
#[derive(Clone, Debug)]
pub struct AppEnv {
pub info: AppInfo,
pub config: Config,
pub curr_web: usize,
pub debug_level: String,
pub appkey: String,
pub checked: bool,
pub collections: HashMap<String, toml::Value>,
pub modules: HashMap<String, Module>,
// pub modules: HashMap<String, toml::Value>,
}
impl Default for AppEnv {
fn default() -> Self {
Self {
info: AppInfo::default(),
config: Config::default(),
curr_web: 0,
debug_level: String::from(""),
appkey: String::from(""),
checked: true,
collections: HashMap::new(),
modules: HashMap::new(),
}
}
}
impl AppEnv {
#[allow(clippy::too_many_arguments)]
pub async fn new(
config: Config,
curr_web: usize,
debug_level: &str,
info: AppInfo,
appkey: &str,
checked: bool,
collections: HashMap<String, toml::Value>,
modules: HashMap<String, Module>,
// jwt_sign: Option<JwtSignature>,
) -> Self {
let akey = appkey.to_string();
Self {
info,
config,
curr_web,
debug_level: debug_level.to_string(),
appkey: akey,
checked,
collections,
modules,
// jwt_sign,
}
}
pub async fn set_appkey(&mut self, app_key: &str, key: &str) {
if key.is_empty() {
self.appkey = app_key.to_string();
}
}
#[must_use]
pub fn has_appkey(&self) -> bool {
self.appkey.as_str() == ""
}
pub fn get_module(&self, key_module: &str) -> Module {
if let Some(module) = self.modules.get(key_module) {
module.to_owned()
} else {
Module::default()
}
}
pub fn get_curr_websrvr_config(&self) -> WebServer {
if let Some(cfg) = self.config.websrvrs.get(self.curr_web) {
cfg.to_owned()
} else {
WebServer::default()
}
}
}