Start dependency injection
This commit is contained in:
parent
d6d8aa7e8a
commit
33e8b74813
@ -10,7 +10,7 @@ namespace ChaosBot.Attribute
|
||||
{
|
||||
public static class AssemblyController
|
||||
{
|
||||
private static Logger _logger = Program._logger;
|
||||
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
public static void RegisterAll()
|
||||
{
|
||||
Assembly dbEntityAssembly = Assembly.GetAssembly(typeof(DBEntity));
|
||||
|
||||
@ -13,7 +13,7 @@ namespace ChaosBot.Database
|
||||
public static class Controller
|
||||
{
|
||||
static SqliteConnection _conn = new SqliteConnection($"Data Source={System.IO.Directory.GetCurrentDirectory()}/{ConfigurationRepository.GetValue<string>("Bot:Database")}");
|
||||
private static Logger _logger = Program._logger;
|
||||
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
/// <summary>
|
||||
/// Run a raw query on the database
|
||||
|
||||
@ -3,11 +3,13 @@ using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using ChaosBot.Database.Entity;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using NLog;
|
||||
|
||||
namespace ChaosBot.Database.Repository
|
||||
{
|
||||
public static class ConfigurationRepository
|
||||
{
|
||||
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
public static IConfiguration AppSettingsHandler { get; set; }
|
||||
|
||||
private static readonly string ServersTable = "ServerConfigurationFlags";
|
||||
@ -32,7 +34,7 @@ namespace ChaosBot.Database.Repository
|
||||
/// <returns></returns>
|
||||
public static T GetValue<T>(string configurationFlag, ulong guildId)
|
||||
{
|
||||
if (Program._logger == null) return AppSettingsHandler.GetValue<T>($"Servers:{guildId}:{configurationFlag}");
|
||||
if (_logger == null) return AppSettingsHandler.GetValue<T>($"Servers:{guildId}:{configurationFlag}");
|
||||
|
||||
string valueSerialized = GetValueRaw(configurationFlag, Convert.ToInt64(guildId));
|
||||
if (valueSerialized != null)
|
||||
@ -54,16 +56,6 @@ namespace ChaosBot.Database.Repository
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Grab configuration from appsettings.json, method does not look at the database
|
||||
/// </summary>
|
||||
/// <param name="configurationFlag"></param>
|
||||
/// <returns></returns>
|
||||
public static IConfigurationSection GetSection(string configurationFlag)
|
||||
{
|
||||
return AppSettingsHandler.GetSection(configurationFlag);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get guild specific configuration option
|
||||
/// </summary>
|
||||
@ -99,7 +91,7 @@ namespace ChaosBot.Database.Repository
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Program._logger.Fatal($"ConfigurationRepository.SetValueRaw: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||
_logger.Fatal($"ConfigurationRepository.SetValueRaw: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
29
ChaosBot/Dependency.cs
Normal file
29
ChaosBot/Dependency.cs
Normal file
@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using NLog;
|
||||
|
||||
namespace ChaosBot
|
||||
{
|
||||
public static class Dependency
|
||||
{
|
||||
private static Dictionary<Type, object> InstanceList;
|
||||
|
||||
public static void Initialize(IConfiguration appSettingsHandler)
|
||||
{
|
||||
InstanceList = new Dictionary<Type, object>
|
||||
{
|
||||
{typeof(ILogger), Logging.GenLog(appSettingsHandler)}
|
||||
};
|
||||
}
|
||||
|
||||
public static T GetInstance<T>()
|
||||
{
|
||||
if (InstanceList.TryGetValue(typeof(T), out object obj))
|
||||
{
|
||||
return (T) obj;
|
||||
}
|
||||
throw new KeyNotFoundException($"Could not find dependency of type {typeof(T)}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -15,7 +15,7 @@ namespace ChaosBot.Discord
|
||||
public class DiscordConnect
|
||||
{
|
||||
private static DiscordSocketClient _client;
|
||||
private static Logger _logger = Program._logger;
|
||||
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
public static async Task StartUp()
|
||||
{
|
||||
|
||||
@ -11,7 +11,7 @@ namespace ChaosBot.Discord.Modules
|
||||
{
|
||||
public class AdminCommands : ModuleBase
|
||||
{
|
||||
private static readonly Logger _logger = Program._logger;
|
||||
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
|
||||
[Command("clear")]
|
||||
|
||||
@ -11,7 +11,7 @@ namespace ChaosBot.Discord.Modules
|
||||
{
|
||||
public class ConfigCommands : ModuleBase
|
||||
{
|
||||
private static readonly Logger _logger = Program._logger;
|
||||
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
private static readonly string _prefix = ConfigurationRepository.GetValue<string>("Discord:Prefix");
|
||||
|
||||
[Command("config")]
|
||||
|
||||
@ -12,7 +12,7 @@ namespace ChaosBot.Discord.Modules
|
||||
{
|
||||
public class DiceCommands : ModuleBase
|
||||
{
|
||||
private static Logger _logger = Program._logger;
|
||||
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
[Command("roll")]
|
||||
public async Task Roll(params string[] args)
|
||||
|
||||
@ -12,7 +12,7 @@ namespace ChaosBot.Discord.Modules
|
||||
{
|
||||
public class InfoCommands : ModuleBase
|
||||
{
|
||||
private static readonly Logger _logger = Program._logger;
|
||||
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
|
||||
[Command("info")]
|
||||
|
||||
@ -17,7 +17,7 @@ namespace ChaosBot.Discord.Modules
|
||||
{
|
||||
public class LodestoneCommands : ModuleBase
|
||||
{
|
||||
private static readonly Logger _logger = Program._logger;
|
||||
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
|
||||
[Command("lodestone character")]
|
||||
|
||||
@ -15,7 +15,7 @@ namespace ChaosBot.Discord.Modules
|
||||
{
|
||||
public class PointsCommands : ModuleBase
|
||||
{
|
||||
private static Logger _logger = Program._logger;
|
||||
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
[Command("points help")]
|
||||
public async Task PointsCommandInfo()
|
||||
|
||||
@ -13,7 +13,7 @@ namespace ChaosBot.Discord.Modules
|
||||
|
||||
public class RaffleSystem : ModuleBase
|
||||
{
|
||||
private static Logger _logger = Program._logger;
|
||||
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
[Command("raffle")]
|
||||
public async Task RaffleStatus()
|
||||
|
||||
@ -4,15 +4,18 @@ using System.Threading.Tasks;
|
||||
using ChaosBot.Database.Repository;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using NLog;
|
||||
|
||||
namespace ChaosBot.Discord.PreConditions
|
||||
{
|
||||
public class CheckCommandPerm : PreconditionAttribute
|
||||
{
|
||||
private static ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
||||
{
|
||||
// Debug information
|
||||
Program._logger.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command.Name}");
|
||||
_logger.Trace($"CheckCommandPerm.CheckPermissionsAsync|Checking permissions for command: {command.Name}");
|
||||
|
||||
// If user is not SocketGuildUser, then return error
|
||||
if (!(context.User is SocketGuildUser gUser)) return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."));
|
||||
@ -46,7 +49,7 @@ namespace ChaosBot.Discord.PreConditions
|
||||
}
|
||||
else
|
||||
{
|
||||
Program._logger.Info("CheckCommandperm.CheckPermissionsAsync|commandPermissions: Null Value");
|
||||
_logger.Info("CheckCommandperm.CheckPermissionsAsync|commandPermissions: Null Value");
|
||||
}
|
||||
|
||||
// Permission denied
|
||||
|
||||
@ -16,7 +16,7 @@ namespace ChaosBot.Discord.Services
|
||||
private readonly CommandService _commands;
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly IServiceProvider _services;
|
||||
private readonly Logger _logger = Program._logger;
|
||||
private readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
public CommandHandler(IServiceProvider services)
|
||||
{
|
||||
|
||||
@ -12,14 +12,14 @@ namespace ChaosBot.Discord.Services
|
||||
{
|
||||
public static class TimerHandler
|
||||
{
|
||||
private static readonly Logger _logger = Program._logger;
|
||||
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
private static DiscordSocketClient _client;
|
||||
|
||||
public static void Initialize(IServiceProvider services)
|
||||
{
|
||||
_client = services.GetRequiredService<DiscordSocketClient>();
|
||||
|
||||
foreach (IConfigurationSection serverConfig in ConfigurationRepository.GetSection("Servers").GetChildren())
|
||||
foreach (IConfigurationSection serverConfig in ConfigurationRepository.AppSettingsHandler.GetSection("Servers").GetChildren())
|
||||
{
|
||||
long? lodestoneChannelSloganDescriptionId = serverConfig.GetValue<long?>("Lodestone:SloganDescription:Channel", null);
|
||||
int refreshMinutes = serverConfig.GetValue<int>("Lodestone:SloganDescription:RefreshMinutes", 60);
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using ChaosBot.Database.Repository;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using NLog;
|
||||
using NLog.Extensions.Logging;
|
||||
|
||||
@ -7,11 +8,11 @@ namespace ChaosBot
|
||||
{
|
||||
public class Logging
|
||||
{
|
||||
public static Logger GenLog()
|
||||
public static Logger GenLog(IConfiguration appSettingsHandler)
|
||||
{
|
||||
try
|
||||
{
|
||||
LogManager.Configuration = new NLogLoggingConfiguration(ConfigurationRepository.GetSection("NLog"));
|
||||
LogManager.Configuration = new NLogLoggingConfiguration(appSettingsHandler.GetSection("NLog"));
|
||||
|
||||
return LogManager.GetCurrentClassLogger();
|
||||
}
|
||||
|
||||
@ -12,12 +12,12 @@ namespace ChaosBot
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
public static Logger _logger;
|
||||
private static string appsettingsPath;
|
||||
private ILogger _logger;
|
||||
private static string _appsettingsPath;
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
appsettingsPath = args.Length > 0 ? args[0] : "./appsettings.json";
|
||||
_appsettingsPath = args.Length > 0 ? args[0] : "./appsettings.json";
|
||||
new Program().MainFunction().GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
@ -28,12 +28,22 @@ namespace ChaosBot
|
||||
/*
|
||||
* Load configuration from AppSettings.Json and save as Cfg
|
||||
*/
|
||||
LoadConfiguration(appsettingsPath);
|
||||
IConfiguration configurationHandler = LoadConfiguration(_appsettingsPath);
|
||||
|
||||
/*
|
||||
* Initialize dependency injector
|
||||
*/
|
||||
Dependency.Initialize(configurationHandler);
|
||||
|
||||
/*
|
||||
* Set AppSettingsHandler on ConfigurationRepository
|
||||
*/
|
||||
ConfigurationRepository.AppSettingsHandler = configurationHandler;
|
||||
|
||||
/*
|
||||
* Initialize the _logger for logging purposes
|
||||
*/
|
||||
LoadLogger();
|
||||
_logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
/*
|
||||
* Attempt to load our custom assemblies
|
||||
@ -56,18 +66,13 @@ namespace ChaosBot
|
||||
}
|
||||
}
|
||||
|
||||
public static void LoadConfiguration(string appsettingsPath)
|
||||
public static IConfiguration LoadConfiguration(string appsettingsPath)
|
||||
{
|
||||
ConfigurationRepository.AppSettingsHandler = new ConfigurationBuilder()
|
||||
return new ConfigurationBuilder()
|
||||
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
|
||||
.AddJsonFile(appsettingsPath, optional: false, reloadOnChange: true).Build();
|
||||
}
|
||||
|
||||
public static void LoadLogger()
|
||||
{
|
||||
_logger = Logging.GenLog();
|
||||
}
|
||||
|
||||
public static void RegisterAssemblyController()
|
||||
{
|
||||
AssemblyController.RegisterAll();
|
||||
|
||||
@ -29,7 +29,7 @@ namespace ChaosBot.Services
|
||||
|
||||
static class LodestoneHttpConnection
|
||||
{
|
||||
private static readonly Logger _logger = Program._logger;
|
||||
private static readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
static HttpClient client = new HttpClient();
|
||||
private static bool _firstRun = true;
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ namespace ChaosBot.WebServer.App.Controller
|
||||
[Route("/config/{guildId}/{flag}")]
|
||||
public class ConfigurationController
|
||||
{
|
||||
private readonly Logger _logger = Program._logger;
|
||||
private readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
[HttpGet]
|
||||
public string GetConfigurationFlag(long guildId, string flag)
|
||||
|
||||
@ -10,7 +10,7 @@ namespace ChaosBot.WebServer
|
||||
{
|
||||
class Startup
|
||||
{
|
||||
private readonly Logger _logger = Program._logger;
|
||||
private readonly ILogger _logger = Dependency.GetInstance<ILogger>();
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
|
||||
@ -31,7 +31,7 @@ namespace ChaosBot.WebServer
|
||||
{
|
||||
logging.ClearProviders();
|
||||
logging.SetMinimumLevel(LogLevel.Trace);
|
||||
logging.AddNLog(new NLogLoggingConfiguration(ConfigurationRepository.GetSection("NLog")));
|
||||
logging.AddNLog(new NLogLoggingConfiguration(ConfigurationRepository.AppSettingsHandler.GetSection("NLog")));
|
||||
})
|
||||
.UseStartup<Startup>();
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user