2024-08-24 19:02:19 +02:00
|
|
|
using Catalogger.Backend.Bot;
|
2024-08-14 16:05:43 +02:00
|
|
|
using Catalogger.Backend.Bot.Commands;
|
2024-08-20 20:25:52 +02:00
|
|
|
using Catalogger.Backend.Bot.Responders.Messages;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Catalogger.Backend.Cache;
|
2024-08-19 16:12:28 +02:00
|
|
|
using Catalogger.Backend.Cache.InMemoryCache;
|
|
|
|
|
using Catalogger.Backend.Cache.RedisCache;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Catalogger.Backend.Database;
|
|
|
|
|
using Catalogger.Backend.Database.Queries;
|
2024-08-16 22:28:05 +02:00
|
|
|
using Catalogger.Backend.Database.Redis;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Catalogger.Backend.Services;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using NodaTime;
|
|
|
|
|
using Remora.Discord.API;
|
|
|
|
|
using Remora.Discord.API.Abstractions.Rest;
|
|
|
|
|
using Remora.Discord.Commands.Services;
|
2024-08-24 19:02:19 +02:00
|
|
|
using Remora.Discord.Gateway.Extensions;
|
2024-08-14 16:05:43 +02:00
|
|
|
using Remora.Discord.Interactivity.Services;
|
|
|
|
|
using Remora.Rest.Core;
|
2024-08-13 13:08:50 +02:00
|
|
|
using Serilog;
|
|
|
|
|
using Serilog.Events;
|
|
|
|
|
|
|
|
|
|
namespace Catalogger.Backend.Extensions;
|
|
|
|
|
|
|
|
|
|
public static class StartupExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds Serilog to this service collection. This method also initializes Serilog, so it should be called as early as possible, before any log calls.
|
|
|
|
|
/// </summary>
|
2024-08-16 22:28:05 +02:00
|
|
|
public static WebApplicationBuilder AddSerilog(this WebApplicationBuilder builder, Config config)
|
2024-08-13 13:08:50 +02:00
|
|
|
{
|
|
|
|
|
var logCfg = new LoggerConfiguration()
|
|
|
|
|
.Enrich.FromLogContext()
|
|
|
|
|
.MinimumLevel.Is(config.Logging.LogEventLevel)
|
|
|
|
|
// ASP.NET's built in request logs are extremely verbose, so we use Serilog's instead.
|
|
|
|
|
// Serilog doesn't disable the built-in logs, so we do it here.
|
|
|
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
|
|
|
|
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command",
|
|
|
|
|
config.Logging.LogQueries ? LogEventLevel.Information : LogEventLevel.Fatal)
|
|
|
|
|
.MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning)
|
|
|
|
|
.MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Warning)
|
|
|
|
|
.MinimumLevel.Override("Microsoft.AspNetCore.Routing", LogEventLevel.Warning)
|
|
|
|
|
.WriteTo.Console();
|
|
|
|
|
|
|
|
|
|
// AddSerilog doesn't seem to add an ILogger to the service collection, so add that manually.
|
|
|
|
|
builder.Services.AddSerilog().AddSingleton(Log.Logger = logCfg.CreateLogger());
|
|
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Config AddConfiguration(this WebApplicationBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
builder.Configuration.Sources.Clear();
|
|
|
|
|
builder.Configuration.AddConfiguration();
|
|
|
|
|
|
|
|
|
|
var config = builder.Configuration.Get<Config>() ?? new();
|
|
|
|
|
builder.Services.AddSingleton(config);
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IConfigurationBuilder AddConfiguration(this IConfigurationBuilder builder)
|
|
|
|
|
{
|
|
|
|
|
var file = Environment.GetEnvironmentVariable("CATALOGGER_CONFIG_FILE") ?? "config.ini";
|
|
|
|
|
|
|
|
|
|
return builder
|
|
|
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
|
|
|
.AddIniFile(file, optional: false, reloadOnChange: false)
|
|
|
|
|
.AddEnvironmentVariables();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServiceCollection AddCustomServices(this IServiceCollection services) => services
|
|
|
|
|
.AddSingleton<IClock>(SystemClock.Instance)
|
2024-08-19 16:12:28 +02:00
|
|
|
.AddSingleton<GuildCache>()
|
|
|
|
|
.AddSingleton<RoleCache>()
|
|
|
|
|
.AddSingleton<ChannelCache>()
|
|
|
|
|
.AddSingleton<UserCache>()
|
2024-08-13 13:08:50 +02:00
|
|
|
.AddSingleton<PluralkitApiService>()
|
|
|
|
|
.AddScoped<IEncryptionService, EncryptionService>()
|
2024-08-20 20:19:24 +02:00
|
|
|
.AddSingleton<MetricsCollectionService>()
|
2024-08-13 13:08:50 +02:00
|
|
|
.AddScoped<MessageRepository>()
|
|
|
|
|
.AddSingleton<WebhookExecutorService>()
|
2024-08-14 16:05:43 +02:00
|
|
|
.AddSingleton<PkMessageHandler>()
|
2024-08-15 17:23:56 +02:00
|
|
|
.AddSingleton(InMemoryDataService<Snowflake, ChannelCommandData>.Instance)
|
2024-08-19 16:12:28 +02:00
|
|
|
.AddSingleton<GuildFetchService>()
|
|
|
|
|
.AddHostedService(serviceProvider => serviceProvider.GetRequiredService<GuildFetchService>());
|
2024-08-13 13:08:50 +02:00
|
|
|
|
2024-08-24 19:02:19 +02:00
|
|
|
public static IHostBuilder AddShardedDiscordService(this IHostBuilder builder,
|
|
|
|
|
Func<IServiceProvider, string> tokenFactory) =>
|
|
|
|
|
builder.ConfigureServices((_, services) => services
|
|
|
|
|
.AddDiscordGateway(tokenFactory)
|
|
|
|
|
.AddSingleton<ShardedGatewayClient>()
|
|
|
|
|
.AddHostedService<ShardedDiscordService>());
|
|
|
|
|
|
2024-08-16 22:28:05 +02:00
|
|
|
public static IServiceCollection MaybeAddRedisCaches(this IServiceCollection services, Config config)
|
|
|
|
|
{
|
|
|
|
|
if (config.Database.Redis == null)
|
|
|
|
|
{
|
2024-08-19 16:12:28 +02:00
|
|
|
return services
|
|
|
|
|
.AddSingleton<IWebhookCache, InMemoryWebhookCache>()
|
|
|
|
|
.AddSingleton<IMemberCache, InMemoryMemberCache>()
|
|
|
|
|
.AddSingleton<IInviteCache, InMemoryInviteCache>();
|
2024-08-16 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return services.AddSingleton<RedisService>()
|
2024-08-19 16:12:28 +02:00
|
|
|
.AddSingleton<IWebhookCache, RedisWebhookCache>()
|
|
|
|
|
.AddSingleton<IMemberCache, RedisMemberCache>()
|
|
|
|
|
.AddSingleton<IInviteCache, RedisInviteCache>();
|
2024-08-16 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
public static async Task Initialize(this WebApplication app)
|
|
|
|
|
{
|
|
|
|
|
await using var scope = app.Services.CreateAsyncScope();
|
|
|
|
|
var logger = scope.ServiceProvider.GetRequiredService<ILogger>().ForContext<Program>();
|
|
|
|
|
logger.Information("Starting Catalogger.NET");
|
|
|
|
|
|
2024-08-21 17:31:39 +02:00
|
|
|
CataloggerMetrics.Startup = scope.ServiceProvider.GetRequiredService<IClock>().GetCurrentInstant();
|
|
|
|
|
|
2024-08-13 13:08:50 +02:00
|
|
|
await using (var db = scope.ServiceProvider.GetRequiredService<DatabaseContext>())
|
|
|
|
|
{
|
|
|
|
|
var migrationCount = (await db.Database.GetPendingMigrationsAsync()).Count();
|
|
|
|
|
if (migrationCount != 0)
|
|
|
|
|
{
|
|
|
|
|
logger.Information("Applying {Count} database migrations", migrationCount);
|
|
|
|
|
await db.Database.MigrateAsync();
|
|
|
|
|
}
|
|
|
|
|
else logger.Information("There are no pending migrations");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var config = scope.ServiceProvider.GetRequiredService<Config>();
|
|
|
|
|
var slashService = scope.ServiceProvider.GetRequiredService<SlashService>();
|
|
|
|
|
|
|
|
|
|
if (config.Discord.ApplicationId == 0)
|
|
|
|
|
{
|
|
|
|
|
logger.Warning(
|
|
|
|
|
"Application ID not set in config. Fetching and setting it now, but for future restarts, please add it to config.ini as Discord.ApplicationId.");
|
|
|
|
|
var restApi = scope.ServiceProvider.GetRequiredService<IDiscordRestApplicationAPI>();
|
|
|
|
|
var application = await restApi.GetCurrentApplicationAsync().GetOrThrow();
|
|
|
|
|
config.Discord.ApplicationId = application.ID.ToUlong();
|
|
|
|
|
logger.Information("Current application ID is {ApplicationId}", config.Discord.ApplicationId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.Discord.SyncCommands)
|
|
|
|
|
{
|
|
|
|
|
if (config.Discord.CommandsGuildId != null)
|
|
|
|
|
{
|
|
|
|
|
logger.Information("Syncing application commands with guild {GuildId}", config.Discord.CommandsGuildId);
|
|
|
|
|
await slashService.UpdateSlashCommandsAsync(
|
|
|
|
|
guildID: DiscordSnowflake.New(config.Discord.CommandsGuildId.Value));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
logger.Information("Syncing application commands globally");
|
|
|
|
|
await slashService.UpdateSlashCommandsAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else logger.Information("Not syncing slash commands, Discord.SyncCommands is false or unset");
|
|
|
|
|
}
|
|
|
|
|
}
|