Skip to content

Commit

Permalink
feat: 优化配置文件导入逻辑,对于未添加 profiles 的文件将默认执行导入逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjing committed Aug 14, 2024
1 parent cd55d09 commit eaa0f83
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 38 deletions.
1 change: 1 addition & 0 deletions examples/db-example/src/role/role_bmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ultimate_db::{base::DbBmc, generate_common_bmc_fns};

use super::model::{RoleEntity, RoleFilter, RoleForCreate, RoleForUpdate};

#[allow(unused)]
pub struct RoleBmc;
impl DbBmc for RoleBmc {
const TABLE: &'static str = "role";
Expand Down
2 changes: 1 addition & 1 deletion ultimates/ultimate-db/src/base/db_bmc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub trait DbBmc {
}

/// 是否过滤用 column id
/// default: fals
/// default: false
fn filter_column_id() -> bool {
false
}
Expand Down
18 changes: 12 additions & 6 deletions ultimates/ultimate-web/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ impl IntoResponse for AppError {
}
}

impl From<hyper::Error> for AppError {
fn from(value: hyper::Error) -> Self {
AppError::new_with_code(500, value.to_string())
}
}

impl From<DataError> for AppError {
fn from(err: DataError) -> Self {
match err {
Expand All @@ -81,6 +75,18 @@ impl From<DataError> for AppError {
}
}

impl From<hyper::Error> for AppError {
fn from(value: hyper::Error) -> Self {
AppError::new_with_code(500, value.to_string())
}
}

impl From<serde_json::Error> for AppError {
fn from(value: serde_json::Error) -> Self {
AppError::new_with_code(500, value.to_string())
}
}

fn convert_security_error(e: security::Error) -> AppError {
// match e {
// security::Error::HmacFailNewFromSlice => todo!(),
Expand Down
66 changes: 36 additions & 30 deletions ultimates/ultimate/src/configuration/util.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,70 @@
use std::{env, path::Path};

use config::{builder::DefaultState, Config, ConfigBuilder, Environment, File, FileFormat};

use tracing::debug;
use log::trace;
use tracing::info;
use ultimate_common::runtime;

use super::Result;

/// 加载配置
///
/// [crate::RunModel]
pub fn load_config() -> Result<Config> {
let files = if let Ok(profiles_active) = env::var("ULTIMATE__PROFILES__ACTIVE") {
let mut b = Config::builder().add_source(load_default_source());

// load from default files, if exists
b = load_from_files(&["app.toml".to_string(), "app.yaml".to_string(), "app.yml".to_string()], b);

// load from profile files, if exists
let profile_files = if let Ok(profiles_active) = env::var("ULTIMATE__PROFILES__ACTIVE") {
vec![
format!("app-{profiles_active}.toml"),
format!("app-{profiles_active}.yaml"),
format!("app-{profiles_active}.yml"),
format!("app-{profiles_active}.toml"),
]
} else {
vec!["app.yaml".to_string(), "app.yml".to_string(), "app.toml".to_string()]
vec![]
};
debug!("Load files: {:?}", files);
trace!("Load files: {:?}", profile_files);
b = load_from_files(&profile_files, b);

let mut config_file = String::new();
let mut b = Config::builder().add_source(load_default_source());
// load from file of env, if exists
if let Ok(file) = std::env::var("ULTIMATE_CONFIG_FILE") {
let path = Path::new(&file);
if path.exists() {
b = b.add_source(File::from(path));
}
}

b = add_enviroment(b);

let c = b.build()?;

for file in &files {
info!("Load config file: {}", c.cache.to_string());

Ok(c)
}

fn load_from_files(files: &[String], mut b: ConfigBuilder<DefaultState>) -> ConfigBuilder<DefaultState> {
for file in files {
if let Ok(path) = runtime::cargo_manifest_dir().map(|dir| dir.join("resources").join(file)) {
if path.exists() {
config_file = format!("{}", path.display());
b = b.add_source(File::from(path));
break;
}
}
}

for file in &files {
for file in files {
let path = Path::new(file);
if path.exists() {
config_file = format!("{}", path.display());
b = b.add_source(File::from(path));
break;
}
}

// load from file of env
if let Ok(file) = std::env::var("ULTIMATE_CONFIG_FILE") {
let path = Path::new(&file);
if path.exists() {
b = b.add_source(File::from(path));
}
config_file = file;
}

b = add_enviroment(b);

{
let ss = format!(r#"ultimate.config_file: "{}""#, config_file);
b = b.add_source(File::from_str(&ss, FileFormat::Yaml));
}

let c = b.build()?;
Ok(c)
b
}

pub fn load_default_source() -> File<config::FileSourceString, FileFormat> {
Expand Down
5 changes: 4 additions & 1 deletion ultimates/ultimate/src/run_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ impl RunMode {
pub fn is_dev(&self) -> bool {
self == &RunMode::DEV
}

pub fn is_test(&self) -> bool {
self == &RunMode::TEST
}
pub fn is_stage(&self) -> bool {

pub fn is_demo(&self) -> bool {
self == &RunMode::DEMO
}

pub fn is_prod(&self) -> bool {
self == &RunMode::PROD
}
Expand Down

0 comments on commit eaa0f83

Please sign in to comment.