From bd0b668c0d4cc28fddb048660d7c301ae72a02e5 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Tue, 15 Nov 2016 10:54:56 +0100 Subject: [PATCH] Configurable connection string --- src/NadekoBot/NadekoBot.cs | 11 ++++------- .../Services/Database/Impl/NadekoSqliteContext.cs | 2 +- src/NadekoBot/Services/DbHandler.cs | 8 +++++--- src/NadekoBot/Services/IBotCredentials.cs | 6 +++--- src/NadekoBot/Services/Impl/BotCredentials.cs | 6 +++--- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/NadekoBot/NadekoBot.cs b/src/NadekoBot/NadekoBot.cs index 5f1f4e79..9138d3bb 100644 --- a/src/NadekoBot/NadekoBot.cs +++ b/src/NadekoBot/NadekoBot.cs @@ -44,6 +44,9 @@ namespace NadekoBot static NadekoBot() { + SetupLogger(); + Credentials = new BotCredentials(); + using (var uow = DbHandler.UnitOfWork()) { AllGuildConfigs = uow.GuildConfigs.GetAll(); @@ -52,16 +55,10 @@ namespace NadekoBot public async Task RunAsync(params string[] args) { - - - SetupLogger(); _log = LogManager.GetCurrentClassLogger(); _log.Info("Starting NadekoBot v" + StatsService.BotVersion); - - Credentials = new BotCredentials(); - //create client Client = new ShardedDiscordClient(new DiscordSocketConfig { @@ -122,7 +119,7 @@ namespace NadekoBot await Task.Delay(-1).ConfigureAwait(false); } - private void SetupLogger() + private static void SetupLogger() { try { diff --git a/src/NadekoBot/Services/Database/Impl/NadekoSqliteContext.cs b/src/NadekoBot/Services/Database/Impl/NadekoSqliteContext.cs index 8af8c84e..c60d0a7c 100644 --- a/src/NadekoBot/Services/Database/Impl/NadekoSqliteContext.cs +++ b/src/NadekoBot/Services/Database/Impl/NadekoSqliteContext.cs @@ -12,7 +12,7 @@ namespace NadekoBot.Services.Database.Impl { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.UseSqlite("Filename=./data/NadekoBot.db"); + optionsBuilder.UseSqlite(NadekoBot.Credentials.Db.ConnectionString); } } } diff --git a/src/NadekoBot/Services/DbHandler.cs b/src/NadekoBot/Services/DbHandler.cs index a2349560..169066ba 100644 --- a/src/NadekoBot/Services/DbHandler.cs +++ b/src/NadekoBot/Services/DbHandler.cs @@ -14,12 +14,14 @@ namespace NadekoBot.Services private static DbHandler _instance = null; public static DbHandler Instance = _instance ?? (_instance = new DbHandler()); - + + private string connectionString { get; } static DbHandler() { } private DbHandler() { dbType = typeof(NadekoSqliteContext); + connectionString = NadekoBot.Credentials.Db.ConnectionString; //switch (NadekoBot.Credentials.Db.Type.ToUpperInvariant()) //{ // case "SQLITE": @@ -34,8 +36,8 @@ namespace NadekoBot.Services //} } - public NadekoContext GetDbContext() => - Activator.CreateInstance(dbType) as NadekoContext; + public NadekoContext GetDbContext() => + new NadekoSqliteContext(); public IUnitOfWork GetUnitOfWork() => new UnitOfWork(GetDbContext()); diff --git a/src/NadekoBot/Services/IBotCredentials.cs b/src/NadekoBot/Services/IBotCredentials.cs index 64b5f7d7..1803c315 100644 --- a/src/NadekoBot/Services/IBotCredentials.cs +++ b/src/NadekoBot/Services/IBotCredentials.cs @@ -15,14 +15,14 @@ namespace NadekoBot.Services string MashapeKey { get; } string LoLApiKey { get; } - DB Db { get; } + DBConfig Db { get; } bool IsOwner(IUser u); } - public class DB + public class DBConfig { - public DB(string type, string connString) + public DBConfig(string type, string connString) { this.Type = type; this.ConnectionString = connString; diff --git a/src/NadekoBot/Services/Impl/BotCredentials.cs b/src/NadekoBot/Services/Impl/BotCredentials.cs index eb7896d6..6f0463b5 100644 --- a/src/NadekoBot/Services/Impl/BotCredentials.cs +++ b/src/NadekoBot/Services/Impl/BotCredentials.cs @@ -28,7 +28,7 @@ namespace NadekoBot.Services.Impl public string OsuApiKey { get; } public string SoundCloudClientId { get; } - public DB Db { get; } + public DBConfig Db { get; } public int TotalShards { get; } public string CarbonKey { get; } @@ -69,7 +69,7 @@ namespace NadekoBot.Services.Impl SoundCloudClientId = data[nameof(SoundCloudClientId)]; CarbonKey = data[nameof(CarbonKey)]; var dbSection = data.GetSection("db"); - Db = new DB(string.IsNullOrWhiteSpace(dbSection["Type"]) + Db = new DBConfig(string.IsNullOrWhiteSpace(dbSection["Type"]) ? "sqlite" : dbSection["Type"], string.IsNullOrWhiteSpace(dbSection["ConnectionString"]) @@ -95,7 +95,7 @@ namespace NadekoBot.Services.Impl public string OsuApiKey { get; set; } = ""; public string SoundCloudClientId { get; set; } = ""; public string CarbonKey { get; set; } = ""; - public DB Db { get; set; } = new DB("sqlite", "Filename=./data/NadekoBot.db"); + public DBConfig Db { get; set; } = new DBConfig("sqlite", "Filename=./data/NadekoBot.db"); public int TotalShards { get; set; } = 1; }