ruwebframe 0.1.2

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

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

use crate::rucfg;
use crate::rulog::rulog;
use serde::Deserialize;
use spicex::Spice;
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use crate::rubase::rudto::cfgdto::CfgDto;

pub fn ifDev() -> bool {
    findEnv() == "dev"
}
pub fn ifTest() -> bool {
    findEnv() == "test"
}
pub fn ifMaster() -> bool {
    findEnv() == "prod" || findEnv() == "master"
}

pub fn ifNotMaster() -> bool {
    !ifMaster()
}

pub fn findEnv() -> String {
    rucfg::find_bean_ruconfig().unwrap().read().software.env
}

#[derive(Debug, serde::Serialize, Deserialize)]
pub struct RuConfig {
    parsed: AtomicBool,
    pub config: Arc<Mutex<CfgDto>>,
}
impl ruentity::BaseEntity for RuConfig {}
impl RuConfig {
    pub fn new() -> Self {
        let rucfg = Self {
            parsed: AtomicBool::new(false),
            config: Arc::new(Mutex::new(cfgdto::CfgDto::default())),
        };
        rucfg.parse_config();
        rucfg
    }

    pub fn parse_config(&self) {
        if self.parsed.load(Ordering::Relaxed) {
            //return;
        }
        let f = format!("./config/{}", self.parse_env2file());
        let file = File::open(&f);
        rulog::info2("f:", f.as_str());
        let mut read = BufReader::new(file.unwrap());
        let mut yaml_str = String::new();
        read.read_to_string(&mut yaml_str).unwrap();
        println!("yaml_str:{:#?}", yaml_str);
        let ret = serde_yaml::from_str::<cfgdto::cfgdto::CfgDto>(&yaml_str);

        // ✅ 显式指定类型
        println!("ret:{:#?}", ret);
        if let Ok(config) = ret {
            let mut guard = self.config.lock().unwrap(); // 获取写锁
            *guard = config;
            guard.parse_env_var();
        } else {
            rulog::error("parse config failed");
        }
        self.parsed.store(true, Ordering::Relaxed);
    }
    pub fn read(&self) -> cfgdto::CfgDto {
        self.config.lock().unwrap().clone()
    }
    pub fn parse_env2file(&self) -> String {
        let env_file = self.parse_env();

        let filename = String::from("ichub-") + &rucfg::parsecfg::parse_env_var(env_file) + ".yml";
        rulog::info2("filename:", filename.as_str());

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

        return env.unwrap();
    }
}

// pub fn parse_config() {
//     let file = File::open("./config/ichub-postgres.yml");
//     let read = BufReader::new(file.unwrap());
//     let mut lines = Vec::<String>::new();
//
//     // 逐行读取
//     for line in read.lines() {
//         let line = line;
//         let aline = line.unwrap();
//         //rulog::info(aline .as_str());
//         lines.push(aline);
//     }
//
//     // 将所有行收集到 Vec<String>,然后合并
//     rulog::info(lines.join("\n").as_str());
// }