Catalogger.NET/Catalogger.Backend/Program.cs

89 lines
3 KiB
C#
Raw Normal View History

2024-08-13 13:08:50 +02:00
using Catalogger.Backend.Bot.Commands;
using Catalogger.Backend.Database;
using Catalogger.Backend.Extensions;
using Catalogger.Backend.Services;
2024-08-13 13:08:50 +02:00
using Newtonsoft.Json.Serialization;
using Prometheus;
2024-08-13 13:08:50 +02:00
using Remora.Commands.Extensions;
using Remora.Discord.API.Abstractions.Gateway.Commands;
using Remora.Discord.Commands.Extensions;
using Remora.Discord.Extensions.Extensions;
using Remora.Discord.Gateway;
using Remora.Discord.Interactivity.Extensions;
using Remora.Discord.Pagination.Extensions;
using Serilog;
using Metrics = Prometheus.Metrics;
2024-08-13 13:08:50 +02:00
var builder = WebApplication.CreateBuilder(args);
var config = builder.AddConfiguration();
builder.AddSerilog(config);
2024-08-13 13:08:50 +02:00
builder.Services
.AddControllers()
.AddNewtonsoftJson(o => o.SerializerSettings.ContractResolver =
new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
});
builder.Host
2024-08-24 19:02:19 +02:00
.AddShardedDiscordService(_ => config.Discord.Token)
2024-08-13 13:08:50 +02:00
.ConfigureServices(s =>
s.AddRespondersFromAssembly(typeof(Program).Assembly)
.Configure<DiscordGatewayClientOptions>(g =>
g.Intents = GatewayIntents.Guilds |
GatewayIntents.GuildBans |
GatewayIntents.GuildInvites |
GatewayIntents.GuildMembers |
GatewayIntents.GuildMessages |
GatewayIntents.GuildWebhooks |
GatewayIntents.MessageContents |
GatewayIntents.GuildEmojisAndStickers)
2024-08-14 16:05:43 +02:00
.AddDiscordCommands(enableSlash: true, useDefaultCommandResponder: false)
2024-08-13 13:08:50 +02:00
.AddCommandTree()
// Start command tree
.WithCommandGroup<MetaCommands>()
2024-08-14 16:05:43 +02:00
.WithCommandGroup<ChannelCommands>()
2024-09-02 15:06:22 +02:00
.WithCommandGroup<KeyRoleCommands>()
2024-08-13 13:08:50 +02:00
// End command tree
.Finish()
.AddPagination()
.AddInteractivity()
2024-08-14 16:05:43 +02:00
.AddInteractionGroup<ChannelCommandsComponents>()
2024-08-13 13:08:50 +02:00
);
// Add metric server
// If metrics are disabled (Logging.EnableMetrics = false), also add a background service that updates
// metrics every minute, as some commands rely on them.
builder.Services.AddMetricServer(o => o.Port = (ushort)config.Logging.MetricsPort);
if (!config.Logging.EnableMetrics)
builder.Services.AddHostedService<BackgroundMetricsCollectionService>();
2024-08-13 13:08:50 +02:00
builder.Services
.AddDbContext<DatabaseContext>()
.MaybeAddRedisCaches(config)
2024-08-13 13:08:50 +02:00
.AddCustomServices()
.AddEndpointsApiExplorer()
.AddSwaggerGen();
var app = builder.Build();
await app.Initialize();
app.UseSerilogRequestLogging();
app.UseRouting();
app.UseHttpMetrics();
2024-08-13 13:08:50 +02:00
app.UseSwagger();
app.UseSwaggerUI();
app.UseCors();
app.MapControllers();
app.Urls.Clear();
app.Urls.Add(config.Web.Address);
// Make sure metrics are updated whenever Prometheus scrapes them
Metrics.DefaultRegistry.AddBeforeCollectCallback(async ct =>
await app.Services.GetRequiredService<MetricsCollectionService>().CollectMetricsAsync(ct));
2024-08-13 13:08:50 +02:00
app.Run();
Log.CloseAndFlush();