Rewrite the WebApi startup to take advantage of new .net6 features
This commit is contained in:
parent
4395d9e9dd
commit
6b3a3a9ec2
2 changed files with 35 additions and 60 deletions
|
@ -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>
|
||||||
|
|
|
@ -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...
|
// Using the "Microsoft.NET.Sdk.Web" SDK requires a static main function...
|
||||||
public static void Main() {}
|
public static void Main()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public static void StartWebApi(IServiceProvider commandProvider, IGeekbotLogger logger, RunParameters runParameters, CommandService commandService,
|
public static void StartWebApi(IServiceProvider commandProvider, IGeekbotLogger logger, RunParameters runParameters, CommandService commandService,
|
||||||
DatabaseContext databaseContext, IGlobalSettings globalSettings, IHighscoreManager highscoreManager, IGuildSettingsManager guildSettingsManager)
|
DatabaseContext databaseContext, IGlobalSettings globalSettings, IHighscoreManager highscoreManager, IGuildSettingsManager guildSettingsManager)
|
||||||
{
|
{
|
||||||
WebHost.CreateDefaultBuilder()
|
var builder = WebApplication.CreateBuilder(new WebApplicationOptions() { ApplicationName = typeof(WebApiStartup).GetTypeInfo().Assembly.FullName });
|
||||||
.UseKestrel(options =>
|
builder.WebHost.UseKestrel(options => options.Listen(IPAddress.Any, int.Parse(runParameters.ApiPort)));
|
||||||
{
|
|
||||||
options.Listen(IPAddress.Any, int.Parse(runParameters.ApiPort));
|
builder.Services.AddControllers();
|
||||||
})
|
builder.Services.AddCors(options => options.AddPolicy("AllowSpecificOrigin", cors => cors.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()));
|
||||||
.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);
|
var interactionCommandManager = new InteractionCommandManager(commandProvider, guildSettingsManager);
|
||||||
|
|
||||||
services.AddSingleton(databaseContext);
|
builder.Services.AddSingleton(databaseContext);
|
||||||
services.AddSingleton(globalSettings);
|
builder.Services.AddSingleton(globalSettings);
|
||||||
services.AddSingleton(highscoreManager);
|
builder.Services.AddSingleton(highscoreManager);
|
||||||
services.AddSingleton(logger);
|
builder.Services.AddSingleton(logger);
|
||||||
services.AddSingleton<IInteractionCommandManager>(interactionCommandManager);
|
builder.Services.AddSingleton<IInteractionCommandManager>(interactionCommandManager);
|
||||||
|
if (!runParameters.DisableGateway) builder.Services.AddSingleton(commandService);
|
||||||
|
|
||||||
if (runParameters.DisableGateway) return;
|
builder.Logging.ClearProviders();
|
||||||
services.AddSingleton(commandService);
|
builder.Logging.SetMinimumLevel(LogLevel.Debug);
|
||||||
})
|
builder.Logging.AddProvider(new AspLogProvider(logger));
|
||||||
.Configure(app =>
|
|
||||||
{
|
var app = builder.Build();
|
||||||
app.UseRouting();
|
app.UseCors(cors => cors.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().Build());
|
||||||
app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().Build());
|
app.MapControllers();
|
||||||
app.UseEndpoints(endpoints =>
|
|
||||||
{
|
app.Run();
|
||||||
endpoints.MapControllers();
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.ConfigureLogging(logging =>
|
|
||||||
{
|
|
||||||
logging.ClearProviders();
|
|
||||||
logging.SetMinimumLevel(LogLevel.Debug);
|
|
||||||
logging.AddProvider(new AspLogProvider(logger));
|
|
||||||
})
|
|
||||||
.UseSetting(WebHostDefaults.ApplicationKey, typeof(WebApiStartup).GetTypeInfo().Assembly.FullName)
|
|
||||||
.Build().Run();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue