diff --git a/ChaosBot/Program.cs b/ChaosBot/Program.cs index 6a1d659..3ec4c58 100644 --- a/ChaosBot/Program.cs +++ b/ChaosBot/Program.cs @@ -48,6 +48,7 @@ namespace ChaosBot _logger.Info($"Starting Up {ConfigurationRepository.GetValue("Bot:Name")} v{ConfigurationRepository.GetValue("Bot:Version")}"); var discordBot = DiscordConnect.StartUp(); + WebServer.WebServer.Start(new string[]{}); await discordBot; } catch (Exception ex) diff --git a/ChaosBot/WebServer/Startup.cs b/ChaosBot/WebServer/Startup.cs new file mode 100644 index 0000000..52a1234 --- /dev/null +++ b/ChaosBot/WebServer/Startup.cs @@ -0,0 +1,47 @@ +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using NLog; + +namespace ChaosBot.WebServer +{ + class Startup + { + private readonly Logger _logger = Program._logger; + + public IConfiguration Configuration { get; } + + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddControllers(); + + services.Configure(options => + { + options.ForwardedHeaders = + ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; + }); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + _logger.Info("Initializing Kestrel Startup and Configuration"); + + app.UseForwardedHeaders(); + app.UseDeveloperExceptionPage(); + app.UseMiddleware(); + app.UseRouting(); + app.UseAuthorization(); + app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); + } + } +} \ No newline at end of file diff --git a/ChaosBot/WebServer/WebServer.cs b/ChaosBot/WebServer/WebServer.cs new file mode 100644 index 0000000..b31b836 --- /dev/null +++ b/ChaosBot/WebServer/WebServer.cs @@ -0,0 +1,39 @@ +using System.Net; +using ChaosBot.Database.Repository; +using NLog.Extensions.Logging; + +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace ChaosBot.WebServer +{ + public static class WebServer + { + public static void Start(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + private static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.ConfigureKestrel(serverOptions => + { + serverOptions.Listen(IPAddress.Any, ConfigurationRepository.GetValue("WebServer:Port"), + listenOptions => + { + listenOptions.UseConnectionLogging(); + }); + }) + .ConfigureLogging(logging => + { + logging.ClearProviders(); + logging.SetMinimumLevel(LogLevel.Trace); + logging.AddNLog(new NLogLoggingConfiguration(ConfigurationRepository.GetSection("NLog"))); + }) + .UseStartup(); + }); + } +} \ No newline at end of file