From d2e268ebf2f2fa7ba36e91252afab2a64271e0bc Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 21 Jul 2024 23:33:22 -0500 Subject: [PATCH 1/5] refine mongo db name --- .../MongoDbContext.cs | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 506477760..189fe54f8 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -1,3 +1,5 @@ +using System.Web; + namespace BotSharp.Plugin.MongoStorage; public class MongoDbContext @@ -6,6 +8,11 @@ public class MongoDbContext private readonly string _mongoDbDatabaseName; private readonly string _collectionPrefix; + private readonly IEnumerable _dbKeys = new List() + { + "authSource" + }; + public MongoDbContext(BotSharpDatabaseSettings dbSettings) { var mongoDbConnectionString = dbSettings.BotSharpMongoDb; @@ -16,14 +23,27 @@ public MongoDbContext(BotSharpDatabaseSettings dbSettings) private string GetDatabaseName(string mongoDbConnectionString) { - var databaseName = mongoDbConnectionString.Substring(mongoDbConnectionString.LastIndexOf("/", StringComparison.InvariantCultureIgnoreCase) + 1); - if (databaseName.Contains("?")) + var dbName = string.Empty; + if (!Uri.TryCreate(mongoDbConnectionString, UriKind.Absolute, out var conn)) { - databaseName = databaseName.Substring(0, databaseName.IndexOf("?", StringComparison.InvariantCultureIgnoreCase)); + return dbName; } - return databaseName; + + var query = HttpUtility.ParseQueryString(conn.Query); + dbName = conn.Segments?.FirstOrDefault(x => x != "/") ?? string.Empty; + var keys = query.AllKeys ?? []; + foreach (var db in _dbKeys) + { + if (keys.Contains(db)) + { + dbName = query[db]; + break; + } + } + return dbName; } + private IMongoDatabase Database { get { return _mongoClient.GetDatabase(_mongoDbDatabaseName); } } #region Indexes From 0d23d00d8d1a68a62c8b20c3289309174fb9c829 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 21 Jul 2024 23:34:50 -0500 Subject: [PATCH 2/5] minor change --- src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 189fe54f8..2119c5d35 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -8,7 +8,7 @@ public class MongoDbContext private readonly string _mongoDbDatabaseName; private readonly string _collectionPrefix; - private readonly IEnumerable _dbKeys = new List() + private readonly IEnumerable _dbIndexes = new List() { "authSource" }; @@ -32,7 +32,7 @@ private string GetDatabaseName(string mongoDbConnectionString) var query = HttpUtility.ParseQueryString(conn.Query); dbName = conn.Segments?.FirstOrDefault(x => x != "/") ?? string.Empty; var keys = query.AllKeys ?? []; - foreach (var db in _dbKeys) + foreach (var db in _dbIndexes) { if (keys.Contains(db)) { From a4ee5ba049e4280b9d923e1656c8ace7da0702a7 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 21 Jul 2024 23:58:40 -0500 Subject: [PATCH 3/5] add to setting --- .../Repositories/BotSharpDatabaseSettings.cs | 8 ++++++- .../MongoDbContext.cs | 21 ++++++++++--------- src/WebStarter/appsettings.json | 5 ++++- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs index ff077296c..1363f5986 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs @@ -4,7 +4,7 @@ public class BotSharpDatabaseSettings : DatabaseBasicSettings { public string[] Assemblies { get; set; } public string FileRepository { get; set; } - public string BotSharpMongoDb { get; set; } + public BotSharpMongoDbSetting BotSharpMongoDb { get; set; } public string TablePrefix { get; set; } public DbConnectionSetting BotSharp { get; set; } public string Redis { get; set; } @@ -29,3 +29,9 @@ public DbConnectionSetting() Slavers = new string[0]; } } + +public class BotSharpMongoDbSetting +{ + public string ConnectionString { get; set; } + public string? DbNameIndex { get; set; } +} \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 2119c5d35..b8771a917 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -15,13 +15,14 @@ public class MongoDbContext public MongoDbContext(BotSharpDatabaseSettings dbSettings) { - var mongoDbConnectionString = dbSettings.BotSharpMongoDb; + var mongoDbConnectionString = dbSettings.BotSharpMongoDb.ConnectionString; + var dbNameIndex = dbSettings.BotSharpMongoDb.DbNameIndex; _mongoClient = new MongoClient(mongoDbConnectionString); - _mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString); + _mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString, dbNameIndex); _collectionPrefix = dbSettings.TablePrefix.IfNullOrEmptyAs("BotSharp"); } - private string GetDatabaseName(string mongoDbConnectionString) + private string GetDatabaseName(string mongoDbConnectionString, string? dbNameIndex = null) { var dbName = string.Empty; if (!Uri.TryCreate(mongoDbConnectionString, UriKind.Absolute, out var conn)) @@ -30,15 +31,15 @@ private string GetDatabaseName(string mongoDbConnectionString) } var query = HttpUtility.ParseQueryString(conn.Query); - dbName = conn.Segments?.FirstOrDefault(x => x != "/") ?? string.Empty; var keys = query.AllKeys ?? []; - foreach (var db in _dbIndexes) + + if (!string.IsNullOrWhiteSpace(dbNameIndex) && keys.Contains(dbNameIndex)) + { + dbName = query[dbNameIndex]; + } + else { - if (keys.Contains(db)) - { - dbName = query[db]; - break; - } + dbName = conn.Segments?.FirstOrDefault(x => x != "/") ?? string.Empty; } return dbName; } diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index a266272ff..c4fd74355 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -226,7 +226,10 @@ "Database": { "Default": "FileRepository", "TablePrefix": "BotSharp", - "BotSharpMongoDb": "", + "BotSharpMongoDb": { + "ConnectionString": "", + "DbIndex": "" + }, "FileRepository": "data", "Assemblies": [ "BotSharp.Core" ] }, From 6554e09a58113d64eb26abc13d752f4314c84f23 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 22 Jul 2024 00:07:46 -0500 Subject: [PATCH 4/5] clean code --- src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index b8771a917..4624f7bbd 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -8,11 +8,6 @@ public class MongoDbContext private readonly string _mongoDbDatabaseName; private readonly string _collectionPrefix; - private readonly IEnumerable _dbIndexes = new List() - { - "authSource" - }; - public MongoDbContext(BotSharpDatabaseSettings dbSettings) { var mongoDbConnectionString = dbSettings.BotSharpMongoDb.ConnectionString; From bf253a84fd2e202cfa763a1f460d4548d01dc0b7 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 22 Jul 2024 17:44:19 -0500 Subject: [PATCH 5/5] get db name --- .../Repositories/BotSharpDatabaseSettings.cs | 8 +------- .../BotSharp.Plugin.MongoStorage/MongoDbContext.cs | 13 +++++++------ src/WebStarter/appsettings.json | 5 +---- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs index 1363f5986..b3c260335 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs @@ -4,7 +4,7 @@ public class BotSharpDatabaseSettings : DatabaseBasicSettings { public string[] Assemblies { get; set; } public string FileRepository { get; set; } - public BotSharpMongoDbSetting BotSharpMongoDb { get; set; } + public string BotSharpMongoDb { get; set; } public string TablePrefix { get; set; } public DbConnectionSetting BotSharp { get; set; } public string Redis { get; set; } @@ -28,10 +28,4 @@ public DbConnectionSetting() { Slavers = new string[0]; } -} - -public class BotSharpMongoDbSetting -{ - public string ConnectionString { get; set; } - public string? DbNameIndex { get; set; } } \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 4624f7bbd..8cc127bdf 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -8,16 +8,17 @@ public class MongoDbContext private readonly string _mongoDbDatabaseName; private readonly string _collectionPrefix; + private const string DB_NAME_INDEX = "authSource"; + public MongoDbContext(BotSharpDatabaseSettings dbSettings) { - var mongoDbConnectionString = dbSettings.BotSharpMongoDb.ConnectionString; - var dbNameIndex = dbSettings.BotSharpMongoDb.DbNameIndex; + var mongoDbConnectionString = dbSettings.BotSharpMongoDb; _mongoClient = new MongoClient(mongoDbConnectionString); - _mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString, dbNameIndex); + _mongoDbDatabaseName = GetDatabaseName(mongoDbConnectionString); _collectionPrefix = dbSettings.TablePrefix.IfNullOrEmptyAs("BotSharp"); } - private string GetDatabaseName(string mongoDbConnectionString, string? dbNameIndex = null) + private string GetDatabaseName(string mongoDbConnectionString) { var dbName = string.Empty; if (!Uri.TryCreate(mongoDbConnectionString, UriKind.Absolute, out var conn)) @@ -28,9 +29,9 @@ private string GetDatabaseName(string mongoDbConnectionString, string? dbNameInd var query = HttpUtility.ParseQueryString(conn.Query); var keys = query.AllKeys ?? []; - if (!string.IsNullOrWhiteSpace(dbNameIndex) && keys.Contains(dbNameIndex)) + if (keys.Contains(DB_NAME_INDEX)) { - dbName = query[dbNameIndex]; + dbName = query[DB_NAME_INDEX]; } else { diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index c4fd74355..a266272ff 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -226,10 +226,7 @@ "Database": { "Default": "FileRepository", "TablePrefix": "BotSharp", - "BotSharpMongoDb": { - "ConnectionString": "", - "DbIndex": "" - }, + "BotSharpMongoDb": "", "FileRepository": "data", "Assemblies": [ "BotSharp.Core" ] },