chore: WebServer in config as vector for Multiples Webs
This commit is contained in:
		
							parent
							
								
									3c0922f8d0
								
							
						
					
					
						commit
						b7733d50e4
					
				@ -7,7 +7,7 @@ use tokio::sync::RwLock;
 | 
			
		||||
use casbin::prelude::*;
 | 
			
		||||
use warp::{Rejection};
 | 
			
		||||
 | 
			
		||||
use app_env::{AppStore,config::Config}; 
 | 
			
		||||
use app_env::{AppStore,config::WebServer}; 
 | 
			
		||||
use app_tools::{trim_newline,from_base64};
 | 
			
		||||
 | 
			
		||||
pub const BEARER_PREFIX: &str = "Bearer ";
 | 
			
		||||
@ -96,7 +96,7 @@ pub struct AuthStore {
 | 
			
		||||
 | 
			
		||||
impl AuthStore {
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn new(config: &Config, enforcer: SharedEnforcer,verbose: &str) -> Self {
 | 
			
		||||
  pub fn new(config: &WebServer, enforcer: SharedEnforcer,verbose: &str) -> Self {
 | 
			
		||||
    Self {
 | 
			
		||||
      users: Arc::new(RwLock::new(AuthStore::create_user_map(config,&verbose))),
 | 
			
		||||
      shadows: Arc::new(RwLock::new(AuthStore::create_shadows_map(config,&verbose))),
 | 
			
		||||
@ -135,7 +135,7 @@ impl AuthStore {
 | 
			
		||||
    shadows
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn create_user_map(config: &Config,verbose: &str) -> HashMap<String, User> {
 | 
			
		||||
  pub fn create_user_map(config: &WebServer,verbose: &str) -> HashMap<String, User> {
 | 
			
		||||
    // TODO  load form YAML o CONFIG 
 | 
			
		||||
    let mut usrs = HashMap::new();
 | 
			
		||||
    match config.usrs_store.as_str() {
 | 
			
		||||
@ -150,7 +150,7 @@ impl AuthStore {
 | 
			
		||||
    usrs
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn create_shadows_map(config: &Config,verbose: &str) -> HashMap<String, UserShadow> {
 | 
			
		||||
  pub fn create_shadows_map(config: &WebServer,verbose: &str) -> HashMap<String, UserShadow> {
 | 
			
		||||
    // TODO  load form YAML o CONFIG 
 | 
			
		||||
    let mut shadows = HashMap::new();
 | 
			
		||||
    match config.usrs_shadow_store.as_str() {
 | 
			
		||||
 | 
			
		||||
@ -30,8 +30,9 @@ impl AppData {
 | 
			
		||||
  /// Schema creation for `AppEnv`
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn new(env: AppEnv) -> Self {
 | 
			
		||||
    let templates_path = env.config.templates_path.to_owned();
 | 
			
		||||
    let default_module = env.config.default_module.to_owned();
 | 
			
		||||
		let config = env.get_curr_websrvr_config();
 | 
			
		||||
    let templates_path = config.templates_path.to_owned();
 | 
			
		||||
    let default_module = config.default_module.to_owned();
 | 
			
		||||
//    let arc_schema = Arc::new(Mutex::from(schema));
 | 
			
		||||
    let mut tera = tera::Tera::default();
 | 
			
		||||
    let mut ctx = tera::Context::new();
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@ use std::collections::HashMap;
 | 
			
		||||
use serde::Deserialize;
 | 
			
		||||
 | 
			
		||||
use crate::{
 | 
			
		||||
  config::Config,
 | 
			
		||||
  config::{Config,WebServer},
 | 
			
		||||
  module::Module,
 | 
			
		||||
	appinfo::AppInfo,
 | 
			
		||||
};
 | 
			
		||||
@ -19,6 +19,7 @@ pub struct DataCollUsers {
 | 
			
		||||
pub struct AppEnv {
 | 
			
		||||
  pub info: AppInfo,
 | 
			
		||||
  pub config: Config,
 | 
			
		||||
	pub curr_web: usize,
 | 
			
		||||
  pub debug_level: String,
 | 
			
		||||
  pub appkey: String,
 | 
			
		||||
  pub checked: bool,
 | 
			
		||||
@ -32,6 +33,7 @@ impl Default for AppEnv {
 | 
			
		||||
    Self {
 | 
			
		||||
      info: AppInfo::default(),
 | 
			
		||||
      config: Config::default(),
 | 
			
		||||
			curr_web: 0,
 | 
			
		||||
      debug_level: String::from(""),
 | 
			
		||||
      appkey: String::from(""),
 | 
			
		||||
      checked: true,
 | 
			
		||||
@ -44,6 +46,7 @@ impl AppEnv {
 | 
			
		||||
  #[allow(clippy::too_many_arguments)]
 | 
			
		||||
  pub async fn new(
 | 
			
		||||
    config: Config,
 | 
			
		||||
		curr_web: usize,
 | 
			
		||||
    debug_level: &str,
 | 
			
		||||
		info: AppInfo,
 | 
			
		||||
    appkey: &str,
 | 
			
		||||
@ -56,6 +59,7 @@ impl AppEnv {
 | 
			
		||||
    Self {
 | 
			
		||||
      info,
 | 
			
		||||
      config,
 | 
			
		||||
			curr_web,
 | 
			
		||||
      debug_level: debug_level.to_string(),
 | 
			
		||||
      appkey: akey,
 | 
			
		||||
      checked,
 | 
			
		||||
@ -80,4 +84,11 @@ impl AppEnv {
 | 
			
		||||
      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()
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -14,6 +14,7 @@ use AppRunMode::{Pro,Premium};
 | 
			
		||||
#[derive(Clone, Debug, Default)]
 | 
			
		||||
pub struct AppInfo {
 | 
			
		||||
  pub name: String,
 | 
			
		||||
	pub webname: String,
 | 
			
		||||
  pub version: String,
 | 
			
		||||
  pub author: String,
 | 
			
		||||
  pub about: String,
 | 
			
		||||
@ -21,15 +22,15 @@ pub struct AppInfo {
 | 
			
		||||
  pub appmode: AppRunMode,
 | 
			
		||||
}
 | 
			
		||||
impl AppInfo {
 | 
			
		||||
	pub async fn new(app_name: &str, version: String, author: String) -> Self {
 | 
			
		||||
	pub async fn new(app_name: &str, webname: String, version: String, author: String) -> Self {
 | 
			
		||||
    let usedata = match std::fs::read_to_string(format!("{}.use",&envmnt::get_or("APP_HOME", ""))) {
 | 
			
		||||
      Ok(res) => res,
 | 
			
		||||
      // Err(e) => {
 | 
			
		||||
      //   println!("Error usedata: {}",e);
 | 
			
		||||
      // Err(e) => { println!("Error usedata: {}",e); String::from("")},
 | 
			
		||||
      Err(_) => String::from(""),
 | 
			
		||||
    };
 | 
			
		||||
    Self {
 | 
			
		||||
      name: format!("{} Server",&app_name),
 | 
			
		||||
			webname,
 | 
			
		||||
      version,
 | 
			
		||||
      author,
 | 
			
		||||
      about: format!("{}: Boot app",&app_name),
 | 
			
		||||
 | 
			
		||||
@ -65,11 +65,10 @@ impl StoreLocal {
 | 
			
		||||
    format!("{}.{}", store, &self.database)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Debug, Deserialize, Default)]
 | 
			
		||||
pub struct Config {
 | 
			
		||||
  pub run_mode: String,
 | 
			
		||||
  pub resources_path: String,
 | 
			
		||||
pub struct WebServer {
 | 
			
		||||
  pub name: String,
 | 
			
		||||
	pub resources_path: String,
 | 
			
		||||
 | 
			
		||||
  pub certs_store_path: String,
 | 
			
		||||
  pub cert_file_sufix: String,
 | 
			
		||||
@ -83,9 +82,7 @@ pub struct Config {
 | 
			
		||||
  pub upload_path: String,
 | 
			
		||||
  /// allow origin for localhost 
 | 
			
		||||
  pub allow_origin: Vec<String>,
 | 
			
		||||
  // warp log name
 | 
			
		||||
  pub log_name: String,
 | 
			
		||||
  /// graphql schemas
 | 
			
		||||
	/// graphql schemas
 | 
			
		||||
  pub gql_schemas_path: String,
 | 
			
		||||
  /// graphql query targets
 | 
			
		||||
  pub gql_targets: String,
 | 
			
		||||
@ -103,6 +100,40 @@ pub struct Config {
 | 
			
		||||
  pub srv_host: String,
 | 
			
		||||
  pub srv_port: u16,
 | 
			
		||||
  pub srv_protocol: String,
 | 
			
		||||
	pub langs: Vec<String>,
 | 
			
		||||
  pub signup_mode: String,
 | 
			
		||||
  pub password_rules: String,
 | 
			
		||||
  pub mapped_url_prefix: String,
 | 
			
		||||
  pub admin_key: String,
 | 
			
		||||
}
 | 
			
		||||
impl WebServer {
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn st_html_path(&self) -> &'static str  {
 | 
			
		||||
    Box::leak(self.html_path.to_owned().into_boxed_str())
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn st_auth_model_path(&self) -> &'static str  {
 | 
			
		||||
    Box::leak(self.auth_model_path.to_owned().into_boxed_str())
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn st_auth_policy_path(&self) -> &'static str  {
 | 
			
		||||
    Box::leak(self.auth_policy_path.to_owned().into_boxed_str())
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn st_gql_req_path(&self) -> &'static str  {
 | 
			
		||||
    Box::leak(self.gql_req_path.to_owned().into_boxed_str())
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn st_giql_req_path(&self) -> &'static str  {
 | 
			
		||||
    Box::leak(self.giql_req_path.to_owned().into_boxed_str())
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Debug, Deserialize, Default)]
 | 
			
		||||
pub struct Config {
 | 
			
		||||
  pub run_mode: String,
 | 
			
		||||
  // warp log name
 | 
			
		||||
  pub log_name: String,
 | 
			
		||||
  pub loop_duration: u64,
 | 
			
		||||
  pub run_cache: bool,
 | 
			
		||||
  pub cache_path: String,
 | 
			
		||||
@ -111,13 +142,9 @@ pub struct Config {
 | 
			
		||||
  pub run_check: bool,
 | 
			
		||||
  pub check_path: String,
 | 
			
		||||
  pub default_lang: String,
 | 
			
		||||
  pub langs: Vec<String>,
 | 
			
		||||
  pub signup_mode: String,
 | 
			
		||||
  pub password_rules: String,
 | 
			
		||||
  pub mapped_url_prefix: String,
 | 
			
		||||
  pub admin_key: String,
 | 
			
		||||
  pub logs_store: String,
 | 
			
		||||
  pub logs_format: String,
 | 
			
		||||
  pub websrvrs: Vec<WebServer>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Config {
 | 
			
		||||
@ -148,17 +175,22 @@ impl Config {
 | 
			
		||||
        	cfg
 | 
			
		||||
				} else {
 | 
			
		||||
					let mut app_cfg = cfg;
 | 
			
		||||
					app_cfg.certs_store_path=format!("{}{}",&app_home,&app_cfg.certs_store_path);
 | 
			
		||||
					app_cfg.resources_path=format!("{}{}",&app_home,&app_cfg.resources_path);
 | 
			
		||||
					app_cfg.templates_path=format!("{}{}",&app_home,&app_cfg.templates_path);
 | 
			
		||||
					app_cfg.defaults_path=format!("{}{}",&app_home,&app_cfg.defaults_path);
 | 
			
		||||
					app_cfg.html_path=format!("{}{}",&app_home,&app_cfg.html_path);
 | 
			
		||||
					app_cfg.dist_path=format!("{}{}",&app_home,&app_cfg.dist_path);
 | 
			
		||||
					app_cfg.upload_path=format!("{}{}",&app_home,&app_cfg.upload_path);
 | 
			
		||||
					app_cfg.auth_model_path=format!("{}{}",&app_home,&app_cfg.auth_model_path);
 | 
			
		||||
					app_cfg.auth_policy_path=format!("{}{}",&app_home,&app_cfg.auth_policy_path);
 | 
			
		||||
					app_cfg.usrs_store_target=format!("{}{}",&app_home,&app_cfg.usrs_store_target);
 | 
			
		||||
					app_cfg.usrs_shadow_target=format!("{}{}",&app_home,&app_cfg.usrs_shadow_target);
 | 
			
		||||
					let mut websrvrs: Vec<WebServer> = Vec::new();
 | 
			
		||||
					app_cfg.websrvrs.iter().enumerate().for_each(|(pos,it)| {
 | 
			
		||||
						websrvrs.push(it.to_owned());
 | 
			
		||||
						websrvrs[pos].certs_store_path=format!("{}{}",&app_home,&it.certs_store_path);
 | 
			
		||||
						websrvrs[pos].resources_path=format!("{}{}",&app_home,&it.resources_path);
 | 
			
		||||
						websrvrs[pos].templates_path=format!("{}{}",&app_home,&it.templates_path);
 | 
			
		||||
						websrvrs[pos].defaults_path=format!("{}{}",&app_home,&it.defaults_path);
 | 
			
		||||
						websrvrs[pos].html_path=format!("{}{}",&app_home,&it.html_path);
 | 
			
		||||
						websrvrs[pos].dist_path=format!("{}{}",&app_home,&it.dist_path);
 | 
			
		||||
						websrvrs[pos].upload_path=format!("{}{}",&app_home,&it.upload_path);
 | 
			
		||||
						websrvrs[pos].auth_model_path=format!("{}{}",&app_home,&it.auth_model_path);
 | 
			
		||||
						websrvrs[pos].auth_policy_path=format!("{}{}",&app_home,&it.auth_policy_path);
 | 
			
		||||
						websrvrs[pos].usrs_store_target=format!("{}{}",&app_home,&it.usrs_store_target);
 | 
			
		||||
						websrvrs[pos].usrs_shadow_target=format!("{}{}",&app_home,&it.usrs_shadow_target);
 | 
			
		||||
					});
 | 
			
		||||
					app_cfg.websrvrs = websrvrs;
 | 
			
		||||
					app_cfg.cache_path=format!("{}{}",&app_home,&app_cfg.cache_path);
 | 
			
		||||
					app_cfg.cache_lock_path=format!("{}{}",&app_home,&app_cfg.cache_lock_path);
 | 
			
		||||
					app_cfg.check_path=format!("{}{}",&app_home,&app_cfg.check_path);
 | 
			
		||||
@ -171,28 +203,8 @@ impl Config {
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn st_html_path(&self) -> &'static str  {
 | 
			
		||||
    Box::leak(self.html_path.to_owned().into_boxed_str())
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn st_auth_model_path(&self) -> &'static str  {
 | 
			
		||||
    Box::leak(self.auth_model_path.to_owned().into_boxed_str())
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn st_auth_policy_path(&self) -> &'static str  {
 | 
			
		||||
    Box::leak(self.auth_policy_path.to_owned().into_boxed_str())
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
	#[must_use]
 | 
			
		||||
  pub fn st_log_name(&self) -> &'static str  {
 | 
			
		||||
    Box::leak(self.log_name.to_owned().into_boxed_str())
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn st_gql_req_path(&self) -> &'static str  {
 | 
			
		||||
    Box::leak(self.gql_req_path.to_owned().into_boxed_str())
 | 
			
		||||
  }
 | 
			
		||||
  #[must_use]
 | 
			
		||||
  pub fn st_giql_req_path(&self) -> &'static str  {
 | 
			
		||||
    Box::leak(self.giql_req_path.to_owned().into_boxed_str())
 | 
			
		||||
  }
 | 
			
		||||
  } 
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user