1. toml依赖
2. 代码
由于项目还未完成,部分配置(如数据库等)还未增加,后续更新增加
| use log::info; |
| use serde::Deserialize; |
| use std::{ |
| fs, sync::LazyLock |
| }; |
| use crate::init::constant::*; |
| |
| static CONFIG: LazyLock<Config> = LazyLock::new(init_config); |
| |
| |
| fn init_config() -> Config { |
| let contents = |
| fs::read_to_string(CONFIG_FILE).expect("Something went wrong reading the config file"); |
| let mut config_input: Config = |
| toml::from_str(&contents).expect(&format!("load {} file failed", CONFIG_FILE)); |
| config_input.server = match config_input.server { |
| None => Some(Server::default()), |
| mut ser => { |
| ser.as_mut().map(|server| { |
| server.ip = server.ip.clone().or_else(|| Server::default().ip); |
| server.port = server.port.or_else(|| Server::default().port); |
| server.name = server.name.clone().or_else(|| Server::default().name); |
| }); |
| ser |
| } |
| }; |
| config_input.nacos = match config_input.nacos { |
| None => Some(Nacos::default()), |
| mut nac => { |
| nac.as_mut().map(|nacos| { |
| nacos.ip = nacos.ip.clone().or_else(|| Nacos::default().ip); |
| nacos.port = nacos.port.or_else(|| Nacos::default().port); |
| nacos.group_name = nacos |
| .group_name |
| .clone() |
| .or_else(|| Nacos::default().group_name); |
| }); |
| nac |
| } |
| }; |
| info!("init config complete"); |
| config_input |
| } |
| |
| #[derive(Deserialize, Debug, Clone, PartialEq)] |
| pub struct Config { |
| pub server: Option<Server>, |
| pub nacos: Option<Nacos>, |
| } |
| |
| #[derive(Deserialize, Debug, Clone, PartialEq)] |
| pub struct Server { |
| pub ip: Option<String>, |
| pub port: Option<u16>, |
| pub name: Option<String>, |
| } |
| |
| #[derive(Deserialize, Debug, Clone, PartialEq)] |
| pub struct Nacos { |
| pub ip: Option<String>, |
| pub port: Option<u16>, |
| pub group_name: Option<String>, |
| } |
| |
| impl Default for Config { |
| fn default() -> Self { |
| Self { |
| server: Some(Server::default()), |
| nacos: Some(Nacos::default()), |
| } |
| } |
| } |
| |
| impl Default for Server { |
| fn default() -> Self { |
| Self { |
| ip: Some(SERVER_DEFAULT_LISTEN.to_owned()), |
| port: Some(SERVER_DEFAULT_PORT), |
| name: Some(SERVER_DEFAULT_NAME.to_owned()), |
| } |
| } |
| } |
| |
| impl Default for Nacos { |
| fn default() -> Self { |
| Self { |
| ip: Some(LOCAL_HOST.to_owned()), |
| port: Some(NACOS_DEFAULT_PORT), |
| group_name: Some(NACOS_DEFAULT_GROUP.to_owned()), |
| } |
| } |
| } |
| |
| impl Config { |
| |
| pub fn global() -> &'static Config{ |
| &CONFIG |
| } |
| |
| |
| |
| pub fn server_ip(&self) -> String { |
| match &self.server { |
| None => Server::default().ip.unwrap(), |
| Some(server) => match &server.ip { |
| None => Server::default().ip.unwrap(), |
| Some(ip) => ip.to_string(), |
| }, |
| } |
| } |
| |
| pub fn server_port(&self) -> u16 { |
| match &self.server { |
| None => Server::default().port.unwrap(), |
| Some(server) => match server.port { |
| None => Server::default().port.unwrap(), |
| Some(port) => port, |
| }, |
| } |
| } |
| |
| pub fn server_name(&self) -> String { |
| match &self.server { |
| None => Server::default().name.unwrap(), |
| Some(server) => match &server.name { |
| None => Server::default().name.unwrap(), |
| Some(name) => name.to_string(), |
| }, |
| } |
| } |
| |
| pub fn nacos_ip(&self) -> String { |
| match &self.nacos { |
| None => Nacos::default().ip.unwrap(), |
| Some(nacos) => match &nacos.ip { |
| None => Nacos::default().ip.unwrap(), |
| Some(ip) => ip.to_string(), |
| }, |
| } |
| } |
| |
| pub fn nacos_port(&self) -> u16 { |
| match &self.nacos { |
| None => Nacos::default().port.unwrap(), |
| Some(nacos) => match nacos.port { |
| None => Nacos::default().port.unwrap(), |
| Some(port) => port, |
| }, |
| } |
| } |
| |
| pub fn nacos_group(&self) -> String { |
| match &self.nacos { |
| None => Nacos::default().group_name.unwrap(), |
| Some(nacos) => match &nacos.group_name { |
| None => Nacos::default().group_name.unwrap(), |
| Some(group_name) => group_name.to_string(), |
| }, |
| } |
| } |
| } |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 易语言 —— 开山篇