From 10db77bcb0eddc09356cc2ea6f08a90bba96ff06 Mon Sep 17 00:00:00 2001 From: phidiaLam <2957035701@qq.com> Date: Wed, 23 Oct 2024 22:01:05 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=91=94=20mongo=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../database/mongo_connection/Cargo.toml | 1 + .../database/mongo_connection/src/config.rs | 6 ++ .../mongo_connection/src/mongo_connect.rs | 95 ++++++++++++++++++- 3 files changed, 97 insertions(+), 5 deletions(-) diff --git a/persistence/database/mongo_connection/Cargo.toml b/persistence/database/mongo_connection/Cargo.toml index bfb4a9f0a..7483972cb 100644 --- a/persistence/database/mongo_connection/Cargo.toml +++ b/persistence/database/mongo_connection/Cargo.toml @@ -15,6 +15,7 @@ async-trait = { workspace = true } database_traits = { path = "../database_traits" } tracing = { workspace = true } time-utils = { workspace = true, features = ["with-mongo"] } +url.workspace = true [dependencies.status-err] path = "../../../libs/status-err" diff --git a/persistence/database/mongo_connection/src/config.rs b/persistence/database/mongo_connection/src/config.rs index 60bd2a728..568043251 100644 --- a/persistence/database/mongo_connection/src/config.rs +++ b/persistence/database/mongo_connection/src/config.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + pub trait DbConnectConfig: serde::de::DeserializeOwned { fn scheme(&self) -> &str; fn username(&self) -> &str; @@ -5,6 +7,7 @@ pub trait DbConnectConfig: serde::de::DeserializeOwned { fn host(&self) -> &str; fn port(&self) -> u16; fn name(&self) -> &str; + fn query(&self) -> &HashMap; } #[derive(Debug, serde::Deserialize)] @@ -16,6 +19,7 @@ pub struct MongoDbConfig { #[serde(default = "port_default")] port: u16, db_name: String, + query: HashMap, } impl DbConnectConfig for MongoDbConfig { @@ -30,6 +34,8 @@ impl DbConnectConfig for MongoDbConfig { fn port(&self) -> u16 { self.port } fn name(&self) -> &str { &self.db_name } + + fn query(&self) -> &HashMap { &self.query} } fn host_default() -> String { "localhost".into() } diff --git a/persistence/database/mongo_connection/src/mongo_connect.rs b/persistence/database/mongo_connection/src/mongo_connect.rs index eb369dee7..940c0c25b 100644 --- a/persistence/database/mongo_connection/src/mongo_connect.rs +++ b/persistence/database/mongo_connection/src/mongo_connect.rs @@ -1,5 +1,6 @@ use mongo_migrate_util::MigratorTrait; use mongodb::{options::ClientOptions, Database}; +use url::{ParseError, Url}; use crate::{ database::builder::DatabaseBuilder, static_vars::set_mongo_database, @@ -52,16 +53,100 @@ async fn init_mongodb(url: &str) -> Result { } fn format_url(cfg: &impl DbConnectConfig) -> String { - let s = format!( - "{}://{}:{}@{}:{}/{}?authSource=admin", + let mut s = Url::parse(&format!( + "{}://{}:{}@{}:{}/{}", cfg.scheme(), cfg.username(), urlencoding::encode(cfg.password()), cfg.host(), cfg.port(), cfg.name() - ); + )) + .expect("MongoDb 连接URL生成异常"); - tracing::info!(mongodb.URL = s); - s + // 添加查询参数 + for (key, value) in cfg.query() { + s.query_pairs_mut().append_pair(key, value); + } + + tracing::info!(mongodb.URL = s.to_string()); + s.to_string() +} + +#[cfg(test)] +mod tests { + + use super::*; + use std::collections::HashMap; + + #[derive(Debug, serde::Deserialize)] + pub struct MongoDbConfig { + username: String, + password: String, + #[serde(default = "host_default")] + host: String, + #[serde(default = "port_default")] + port: u16, + db_name: String, + query: HashMap, + } + + impl DbConnectConfig for MongoDbConfig { + fn scheme(&self) -> &str { + "mongodb" + } + + fn username(&self) -> &str { + &self.username + } + + fn password(&self) -> &str { + &self.password + } + + fn host(&self) -> &str { + &self.host + } + + fn port(&self) -> u16 { + self.port + } + + fn name(&self) -> &str { + &self.db_name + } + + fn query(&self) -> &HashMap { + &self.query + } + } + + fn host_default() -> String { + "localhost".into() + } + + fn port_default() -> u16 { + 27017 + } + + #[test] + fn test_format_url() { + let mut query = HashMap::new(); + query.insert("authSource".to_string(), "admin".to_string()); + query.insert("directConnection".to_string(), "true".to_string()); + + let config = MongoDbConfig { + username: "user".to_string(), + password: "password".to_string(), + host: "localhost".to_string(), + port: 27017, + db_name: "mydb".to_string(), + query, + }; + + let expected_url = + "mongodb://user:password@localhost:27017/mydb?authSource=admin&directConnection=true"; + let result = format_url(&config); + assert_eq!(result, expected_url); + } } From 0735d3e5d1ab1b845e8dbcdc0866b0befed02784 Mon Sep 17 00:00:00 2001 From: phidiaLam <2957035701@qq.com> Date: Wed, 23 Oct 2024 22:05:19 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9D=20=E6=9B=B4=E6=96=B0readme?= =?UTF-8?q?=E5=B9=B6=E4=B8=94=E6=B7=BB=E5=8A=A0mongo=20query=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + persistence/database/mongo_connection/src/config.rs | 3 +++ 2 files changed, 4 insertions(+) diff --git a/README.md b/README.md index 695a75fbc..810518360 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ | `mongodb`/ `mongo` | `host` | 否 | `String` | Mongodb 进行数据库连接使用的 host | 默认为`localhost` | | `mongodb`/ `mongo` | `port` | 否 | `String` | Mongodb 进行数据库连接使用的端口 | 默认为`27017` | | `mongodb`/ `mongo` | `db_name` | 是 | `String` | Mongodb 进行数据库连接使用的数据库 | 无 | + | `mongodb`/ `mongo` | `query` | 否 | `HashMap` | Mongodb 进行数据库连接使用的参数 | 默认为`{}` | | `user_auth`/ `auth` | `jwt`/`jwt-key` | 否 | `String` | 用户鉴权使用的`Jwt`密钥 | 最大长度不超过 32 位。过长部分将会被截断,过短部分将会被随机数填充 | | `user_auth`/ `auth` | `header`/`header_name` | 否 | `String` | 获取 token 的 Header | 默认为`Token` | | `user_auth`/ `auth` | `mob_header` | 否 | `String` | 获取 mob_id 的 Header | 默认为`mob-id` | diff --git a/persistence/database/mongo_connection/src/config.rs b/persistence/database/mongo_connection/src/config.rs index 568043251..b4d3d98d7 100644 --- a/persistence/database/mongo_connection/src/config.rs +++ b/persistence/database/mongo_connection/src/config.rs @@ -19,6 +19,7 @@ pub struct MongoDbConfig { #[serde(default = "port_default")] port: u16, db_name: String, + #[serde(default = "query_default")] query: HashMap, } @@ -41,3 +42,5 @@ impl DbConnectConfig for MongoDbConfig { fn host_default() -> String { "localhost".into() } fn port_default() -> u16 { 27017 } + +fn query_default() -> HashMap { HashMap::new() } From 082b206a4dbfd829a9273185cce18cb128d238d0 Mon Sep 17 00:00:00 2001 From: phidiaLam <2957035701@qq.com> Date: Wed, 23 Oct 2024 22:08:57 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=8E=A8=20cargo=20clippy=20&=20fmt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../database/mongo_connection/src/config.rs | 2 +- .../mongo_connection/src/mongo_connect.rs | 45 ++++++------------- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/persistence/database/mongo_connection/src/config.rs b/persistence/database/mongo_connection/src/config.rs index b4d3d98d7..35e008a77 100644 --- a/persistence/database/mongo_connection/src/config.rs +++ b/persistence/database/mongo_connection/src/config.rs @@ -36,7 +36,7 @@ impl DbConnectConfig for MongoDbConfig { fn name(&self) -> &str { &self.db_name } - fn query(&self) -> &HashMap { &self.query} + fn query(&self) -> &HashMap { &self.query } } fn host_default() -> String { "localhost".into() } diff --git a/persistence/database/mongo_connection/src/mongo_connect.rs b/persistence/database/mongo_connection/src/mongo_connect.rs index 940c0c25b..d5618d0fd 100644 --- a/persistence/database/mongo_connection/src/mongo_connect.rs +++ b/persistence/database/mongo_connection/src/mongo_connect.rs @@ -1,6 +1,6 @@ use mongo_migrate_util::MigratorTrait; use mongodb::{options::ClientOptions, Database}; -use url::{ParseError, Url}; +use url::Url; use crate::{ database::builder::DatabaseBuilder, static_vars::set_mongo_database, @@ -76,9 +76,10 @@ fn format_url(cfg: &impl DbConnectConfig) -> String { #[cfg(test)] mod tests { - use super::*; use std::collections::HashMap; + use super::*; + #[derive(Debug, serde::Deserialize)] pub struct MongoDbConfig { username: String, @@ -92,42 +93,24 @@ mod tests { } impl DbConnectConfig for MongoDbConfig { - fn scheme(&self) -> &str { - "mongodb" - } + fn scheme(&self) -> &str { "mongodb" } - fn username(&self) -> &str { - &self.username - } + fn username(&self) -> &str { &self.username } - fn password(&self) -> &str { - &self.password - } + fn password(&self) -> &str { &self.password } - fn host(&self) -> &str { - &self.host - } + fn host(&self) -> &str { &self.host } - fn port(&self) -> u16 { - self.port - } + fn port(&self) -> u16 { self.port } - fn name(&self) -> &str { - &self.db_name - } + fn name(&self) -> &str { &self.db_name } - fn query(&self) -> &HashMap { - &self.query - } + fn query(&self) -> &HashMap { &self.query } } - fn host_default() -> String { - "localhost".into() - } + fn host_default() -> String { "localhost".into() } - fn port_default() -> u16 { - 27017 - } + fn port_default() -> u16 { 27017 } #[test] fn test_format_url() { @@ -144,8 +127,8 @@ mod tests { query, }; - let expected_url = - "mongodb://user:password@localhost:27017/mydb?authSource=admin&directConnection=true"; + let expected_url = "mongodb://user:password@localhost:27017/mydb?\ + authSource=admin&directConnection=true"; let result = format_url(&config); assert_eq!(result, expected_url); }