Refactor Program Main class
This commit is contained in:
parent
78038f8991
commit
76773074ad
@ -1,5 +1,6 @@
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ChaosBot.Discord;
|
||||
using System.Threading.Tasks;
|
||||
@ -8,17 +9,51 @@ using Microsoft.Extensions.Configuration;
|
||||
[assembly: InternalsVisibleTo("ChaosBot.UnitTests")]
|
||||
namespace ChaosBot
|
||||
{
|
||||
internal class Program
|
||||
internal static class Program
|
||||
{
|
||||
private static ILogger _logger;
|
||||
public static IConfiguration AppSettingsHandler;
|
||||
|
||||
private static string _appsettingsPath;
|
||||
private static string _appSettingsPath;
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
_appsettingsPath = args.Length > 0 ? args[0] : "./appsettings.json";
|
||||
new Program().MainFunction().GetAwaiter().GetResult();
|
||||
_appSettingsPath = args.Length > 0 ? args[0] : "./appsettings.json";
|
||||
|
||||
try
|
||||
{
|
||||
/*
|
||||
* Load configuration from AppSettings.Json and save as Cfg
|
||||
*/
|
||||
AppSettingsHandler = LoadConfiguration(_appSettingsPath);
|
||||
|
||||
/*
|
||||
* Initialize the _logger for logging purposes
|
||||
*/
|
||||
_logger = Logging.GenLog(AppSettingsHandler);
|
||||
_logger.Trace("Logger initialized");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.Write($"{MethodBase.GetCurrentMethod()?.ReflectedType?.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.\n{ex.StackTrace}");
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the Discord Client and Login
|
||||
*/
|
||||
_logger.Info($"Starting Up {AppSettingsHandler.GetValue<string>("Bot:Name")} v{AppSettingsHandler.GetValue<string>("Bot:Version")}");
|
||||
|
||||
try
|
||||
{
|
||||
Task discordBot = LoadDiscord();
|
||||
LoadWebServer(args);
|
||||
discordBot.GetAwaiter().GetResult();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LoggingFacade.Exception(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static ILogger GetLogger()
|
||||
@ -26,54 +61,19 @@ namespace ChaosBot
|
||||
return _logger;
|
||||
}
|
||||
|
||||
private async Task MainFunction()
|
||||
{
|
||||
try
|
||||
{
|
||||
/*
|
||||
* Load configuration from AppSettings.Json and save as Cfg
|
||||
*/
|
||||
IConfiguration configurationHandler = LoadConfiguration(_appsettingsPath);
|
||||
|
||||
/*
|
||||
* Initialize the _logger for logging purposes
|
||||
*/
|
||||
_logger = Logging.GenLog(configurationHandler);
|
||||
|
||||
/*
|
||||
* Set AppSettingsHandler on ConfigurationRepository
|
||||
*/
|
||||
AppSettingsHandler = configurationHandler;
|
||||
|
||||
/*
|
||||
* Initialize the Discord Client and Login
|
||||
*/
|
||||
_logger.Info($"Starting Up {AppSettingsHandler.GetValue<string>("Bot:Name")} v{AppSettingsHandler.GetValue<string>("Bot:Version")}");
|
||||
|
||||
|
||||
var discordBot = LoadDiscord();
|
||||
LoadWebServer();
|
||||
await discordBot;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error(ex, $"Program.MainFunction: Exception [{ex}] thrown, <[{ex.Message}]>.");
|
||||
}
|
||||
}
|
||||
|
||||
public static IConfiguration LoadConfiguration(string appsettingsPath)
|
||||
private static IConfiguration LoadConfiguration(string appSettingsPath)
|
||||
{
|
||||
return new ConfigurationBuilder()
|
||||
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
|
||||
.AddJsonFile(appsettingsPath, optional: false, reloadOnChange: true).Build();
|
||||
.AddJsonFile(appSettingsPath, optional: false, reloadOnChange: true).Build();
|
||||
}
|
||||
|
||||
public static void LoadWebServer()
|
||||
private static void LoadWebServer(string[] args)
|
||||
{
|
||||
WebServer.WebServer.Start(new string[]{});
|
||||
WebServer.WebServer.Start(args);
|
||||
}
|
||||
|
||||
public static Task LoadDiscord()
|
||||
private static Task LoadDiscord()
|
||||
{
|
||||
return DiscordConnect.StartUp();
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user