feat: start dashboard

This commit is contained in:
sam 2024-10-18 22:13:23 +02:00
parent bacbc6db0e
commit ec7aa9faba
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
50 changed files with 3624 additions and 18 deletions

View file

@ -13,6 +13,8 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
using Catalogger.Backend.Api;
using Catalogger.Backend.Api.Middleware;
using Catalogger.Backend.Bot;
using Catalogger.Backend.Bot.Commands;
using Catalogger.Backend.Bot.Responders.Messages;
@ -25,6 +27,7 @@ using Catalogger.Backend.Database.Redis;
using Catalogger.Backend.Services;
using Microsoft.EntityFrameworkCore;
using NodaTime;
using Prometheus;
using Remora.Discord.API;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.Commands.Services;
@ -115,6 +118,7 @@ public static class StartupExtensions
.AddSingleton(InMemoryDataService<Snowflake, ChannelCommandData>.Instance)
.AddSingleton<GuildFetchService>()
.AddTransient<PermissionResolverService>()
// Background services
// GuildFetchService is added as a separate singleton as it's also injected into other services.
.AddHostedService(serviceProvider =>
serviceProvider.GetRequiredService<GuildFetchService>()
@ -134,6 +138,26 @@ public static class StartupExtensions
.AddHostedService<ShardedDiscordService>()
);
/// <summary>
/// The dashboard API is only enabled when Redis is configured, as it heavily relies on it.
/// This method only adds API-related services when Redis is found as otherwise we'll get missing dependency errors.
/// The actual API definition
/// </summary>
public static IServiceCollection MaybeAddDashboardServices(
this IServiceCollection services,
Config config
)
{
if (config.Database.Redis == null)
return services;
return services
.AddScoped<ApiCache>()
.AddScoped<DiscordRequestService>()
.AddScoped<AuthenticationMiddleware>()
.AddScoped<ErrorMiddleware>();
}
public static IServiceCollection MaybeAddRedisCaches(
this IServiceCollection services,
Config config
@ -216,4 +240,29 @@ public static class StartupExtensions
"Not syncing slash commands, Discord.SyncCommands is false or unset"
);
}
public static void MaybeAddDashboard(this WebApplication app)
{
using var scope = app.Services.CreateScope();
var logger = scope.ServiceProvider.GetRequiredService<ILogger>().ForContext<Program>();
var config = scope.ServiceProvider.GetRequiredService<Config>();
if (config.Database.Redis == null)
{
logger.Warning(
"Redis URL is not set. The dashboard relies on Redis, so it will not be usable."
);
return;
}
app.UseSerilogRequestLogging();
app.UseRouting();
app.UseHttpMetrics();
app.UseSwagger();
app.UseSwaggerUI();
app.UseCors();
app.UseMiddleware<ErrorMiddleware>();
app.UseMiddleware<AuthenticationMiddleware>();
app.MapControllers();
}
}