ruwebframe 0.1.10

a simple webframe for rust actix-web, based on rudi and rbatis.
Documentation
use crate::rubase::{BaseEntitySingle, rutils, BaseEntity};

use crate::rubase::ruconst::ruconst;
use crate::rubase::rudto::cfgdto;

use crate::rubase::rudto::cfgdto::CfgDto;
use crate::rubase::rutils::{ru_file, str_utils};

use crate::rulog::ru_log;
use serde::Deserialize;
use spicex::Spice;
use std::fs::File;
use std::io::{BufReader, Read};
use std::sync::{ Mutex};
use crate::rubase::get_self::GetSelf;
use crate::ruconf::find_bean_ru_config;
use crate::rulog;

pub fn if_dev() -> bool {
    if_env("dev")
}

pub fn if_test() -> bool {
    if_env("test")
}
pub fn if_release() -> bool {
    if_env("release")
}

pub fn if_master() -> bool {
    if_env("prod") || if_env("master")
}

pub fn if_not_master() -> bool {
    !if_master()
}

// 干净的函数别名,零额外开销
pub fn find_env() -> String {
   find_bean_ru_config().unwrap().lock().unwrap().find_env()
}
pub fn if_env(env: &str) -> bool {
    find_bean_ru_config().unwrap().lock().unwrap().if_env(env)
}

#[derive(Debug,Clone, serde::Serialize, Deserialize)]
pub struct RuConfig {

    pub config: CfgDto,
    pub base_path: String,
}
impl BaseEntitySingle for RuConfig {}

impl GetSelf for RuConfig {}
impl RuConfig {
    pub fn new() -> Self {
        let mut ruconf = Self {
            config: CfgDto::default(),
            base_path: "".to_string(),
        };
        ruconf.parse_config();
        ruconf
    }
    pub fn init(&mut self){
        self.base_path = rutils::get_config_path();
        rulog::info2("base_path:", self.base_path.as_str());
    }
    pub fn new_mutex() -> Mutex<RuConfig> {
        Mutex::new(Self::new())
    }
    pub fn get_config(&self, file: String) -> String {
        let mut cfgdir = self.base_path.clone();
        cfgdir.push_str("/config/");
        cfgdir.push_str(file.as_str());

        cfgdir
    }
    pub fn parse_config(&mut self) {
        let envFile = self.get_config(self.parse_env2file());
        let file = File::open(&envFile);

        let mut read = BufReader::new(file.unwrap());
        let mut yaml_str = String::new();
        read.read_to_string(&mut yaml_str).unwrap();

        let ret = serde_yaml::from_str::<CfgDto>(&yaml_str);
        // ✅ 显式指定类型
        if let Ok(mut config) = ret {
            config.parse_env_var();
            self.config = config;
        } else {
            ru_log::error("parse config failed" );
            print!("{:#?}", ret);
        }

    }

    pub fn find_env(&self) -> String {
        self.config.find_env()
    }

    pub fn parse_env2file(&self) -> String {
        let env_file = self.parse_env();
        let filename = String::from("ichub-") + &rutils::parse_env_var_only(env_file) + ".yml";
        ru_log::info2("filename:", filename.as_str());

        return filename;
    }
    pub fn parse_env(&self) -> String {
        let mut spice = Spice::new();
        spice
            .set_config_file(rutils::find_base_path() + ruconst::FILE_PATH_ENV)
            .unwrap();
        let env = spice.get_string("Env").unwrap();

        return env.unwrap();
    }
    pub fn read_json(&self) -> & CfgDto {
        self.read()
    }
    // pub fn get_self(&self) -> &CfgDto {
    //     &self.config
    // }

    pub fn find_base_path(&self) -> String {
        ru_file::find_base_path()
    }
    pub fn read_clone(&self) -> cfgdto::CfgDto {
        self.config.clone()
    }
    pub fn read(&self) -> & CfgDto {
        &self.config
    }

    pub fn if_env(&self, env: &str) -> bool {
        self.find_env().as_str() == env
    }
    pub fn if_master(&self) -> bool {
       self. if_env("prod") || self.if_env("master")
    }

    pub fn if_not_master(&self) -> bool {
        !self.if_master()
    }

    pub fn get_web_port(&self) -> u16 {

        if self.if_not_master() {
            ru_log::info2("ruwebframe config=>", self.config.clone());
        }
        str_utils::str2u16(self.config.web.server.port.as_str())
    }
}