Add basic webserver setup

This commit is contained in:
Daniel_I_Am 2020-06-06 15:49:39 +02:00
parent 8682cc99bc
commit b2f859f817
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
3 changed files with 87 additions and 0 deletions

View File

@ -48,6 +48,7 @@ namespace ChaosBot
_logger.Info($"Starting Up {ConfigurationRepository.GetValue<string>("Bot:Name")} v{ConfigurationRepository.GetValue<string>("Bot:Version")}"); _logger.Info($"Starting Up {ConfigurationRepository.GetValue<string>("Bot:Name")} v{ConfigurationRepository.GetValue<string>("Bot:Version")}");
var discordBot = DiscordConnect.StartUp(); var discordBot = DiscordConnect.StartUp();
WebServer.WebServer.Start(new string[]{});
await discordBot; await discordBot;
} }
catch (Exception ex) catch (Exception ex)

View File

@ -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<ForwardedHeadersOptions>(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<AuthenticationMiddleware>();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}
}

View File

@ -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<int>("WebServer:Port"),
listenOptions =>
{
listenOptions.UseConnectionLogging();
});
})
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(LogLevel.Trace);
logging.AddNLog(new NLogLoggingConfiguration(ConfigurationRepository.GetSection("NLog")));
})
.UseStartup<Startup>();
});
}
}