Refactor Program Main class
This commit is contained in:
parent
78038f8991
commit
76773074ad
@ -1,5 +1,6 @@
|
|||||||
using NLog;
|
using NLog;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using ChaosBot.Discord;
|
using ChaosBot.Discord;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -8,17 +9,51 @@ using Microsoft.Extensions.Configuration;
|
|||||||
[assembly: InternalsVisibleTo("ChaosBot.UnitTests")]
|
[assembly: InternalsVisibleTo("ChaosBot.UnitTests")]
|
||||||
namespace ChaosBot
|
namespace ChaosBot
|
||||||
{
|
{
|
||||||
internal class Program
|
internal static class Program
|
||||||
{
|
{
|
||||||
private static ILogger _logger;
|
private static ILogger _logger;
|
||||||
public static IConfiguration AppSettingsHandler;
|
public static IConfiguration AppSettingsHandler;
|
||||||
|
|
||||||
private static string _appsettingsPath;
|
private static string _appSettingsPath;
|
||||||
|
|
||||||
private static void Main(string[] args)
|
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();
|
|
||||||
|
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()
|
public static ILogger GetLogger()
|
||||||
@ -26,54 +61,19 @@ namespace ChaosBot
|
|||||||
return _logger;
|
return _logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task MainFunction()
|
private static IConfiguration LoadConfiguration(string appSettingsPath)
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
return new ConfigurationBuilder()
|
return new ConfigurationBuilder()
|
||||||
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
|
.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();
|
return DiscordConnect.StartUp();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user