Rewrite the WebApi startup to take advantage of new .net6 features

This commit is contained in:
Daan Boerlage 2021-11-07 00:02:05 +01:00
parent 4395d9e9dd
commit 6b3a3a9ec2
Signed by: daan
GPG key ID: FCE070E1E4956606
2 changed files with 35 additions and 60 deletions

View file

@ -10,6 +10,9 @@
<RootNamespace>Geekbot.Web</RootNamespace> <RootNamespace>Geekbot.Web</RootNamespace>
<AssemblyName>Geekbot.Web</AssemblyName> <AssemblyName>Geekbot.Web</AssemblyName>
<NoWarn>NU1701</NoWarn> <NoWarn>NU1701</NoWarn>
<NoWarn>CS8618</NoWarn>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View file

@ -1,8 +1,6 @@
using System; using System.Net;
using System.Net;
using System.Reflection; using System.Reflection;
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket;
using Geekbot.Core; using Geekbot.Core;
using Geekbot.Core.Database; using Geekbot.Core.Database;
using Geekbot.Core.GlobalSettings; using Geekbot.Core.GlobalSettings;
@ -11,68 +9,42 @@ using Geekbot.Core.Highscores;
using Geekbot.Core.Interactions; using Geekbot.Core.Interactions;
using Geekbot.Core.Logger; using Geekbot.Core.Logger;
using Geekbot.Web.Logging; using Geekbot.Web.Logging;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Geekbot.Web namespace Geekbot.Web;
public static class WebApiStartup
{ {
public static class WebApiStartup // Using the "Microsoft.NET.Sdk.Web" SDK requires a static main function...
public static void Main()
{ {
// Using the "Microsoft.NET.Sdk.Web" SDK requires a static main function... }
public static void Main() {}
public static void StartWebApi(IServiceProvider commandProvider, IGeekbotLogger logger, RunParameters runParameters, CommandService commandService,
DatabaseContext databaseContext, IGlobalSettings globalSettings, IHighscoreManager highscoreManager, IGuildSettingsManager guildSettingsManager)
{
WebHost.CreateDefaultBuilder()
.UseKestrel(options =>
{
options.Listen(IPAddress.Any, int.Parse(runParameters.ApiPort));
})
.ConfigureServices(services =>
{
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
});
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
});
services.AddSentry();
var interactionCommandManager = new InteractionCommandManager(commandProvider, guildSettingsManager); public static void StartWebApi(IServiceProvider commandProvider, IGeekbotLogger logger, RunParameters runParameters, CommandService commandService,
DatabaseContext databaseContext, IGlobalSettings globalSettings, IHighscoreManager highscoreManager, IGuildSettingsManager guildSettingsManager)
{
var builder = WebApplication.CreateBuilder(new WebApplicationOptions() { ApplicationName = typeof(WebApiStartup).GetTypeInfo().Assembly.FullName });
builder.WebHost.UseKestrel(options => options.Listen(IPAddress.Any, int.Parse(runParameters.ApiPort)));
services.AddSingleton(databaseContext); builder.Services.AddControllers();
services.AddSingleton(globalSettings); builder.Services.AddCors(options => options.AddPolicy("AllowSpecificOrigin", cors => cors.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()));
services.AddSingleton(highscoreManager);
services.AddSingleton(logger);
services.AddSingleton<IInteractionCommandManager>(interactionCommandManager);
if (runParameters.DisableGateway) return; var interactionCommandManager = new InteractionCommandManager(commandProvider, guildSettingsManager);
services.AddSingleton(commandService);
}) builder.Services.AddSingleton(databaseContext);
.Configure(app => builder.Services.AddSingleton(globalSettings);
{ builder.Services.AddSingleton(highscoreManager);
app.UseRouting(); builder.Services.AddSingleton(logger);
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().Build()); builder.Services.AddSingleton<IInteractionCommandManager>(interactionCommandManager);
app.UseEndpoints(endpoints => if (!runParameters.DisableGateway) builder.Services.AddSingleton(commandService);
{
endpoints.MapControllers(); builder.Logging.ClearProviders();
}); builder.Logging.SetMinimumLevel(LogLevel.Debug);
}) builder.Logging.AddProvider(new AspLogProvider(logger));
.ConfigureLogging(logging =>
{ var app = builder.Build();
logging.ClearProviders(); app.UseCors(cors => cors.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().Build());
logging.SetMinimumLevel(LogLevel.Debug); app.MapControllers();
logging.AddProvider(new AspLogProvider(logger));
}) app.Run();
.UseSetting(WebHostDefaults.ApplicationKey, typeof(WebApiStartup).GetTypeInfo().Assembly.FullName)
.Build().Run();
}
} }
} }