chaosbot/ChaosBot/WebServer/Startup.cs

62 lines
2.1 KiB
C#

using System.Net.Http;
using ChaosBot.WebServer.Services;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace ChaosBot.WebServer
{
class Startup
{
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;
});
services
.AddSingleton(sp => new AccessTokenCache())
.AddSingleton(sp => new DiscordInviteGenerator())
.AddSingleton(sp => new HttpClient())
.AddSingleton(sp => new ValidationService())
;
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
LoggingFacade.Info("Initializing Kestrel Startup and Configuration");
if (new Configuration().GetValueGlobalDefault<bool>("WebServer:Debug"))
app.UseDeveloperExceptionPage();
app.UseForwardedHeaders();
app.UseMiddleware<AuthenticationMiddleware>();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("index.html");
app.UseDefaultFiles(options);
app.UseStaticFiles();
}
}
}