using Flypaper.Core.Abstractions; using Flypaper.Storage.Dashboard; using Flypaper.Storage.Events; using Flypaper.Storage.Maintenance; using Flypaper.Storage.Queries; using Flypaper.Storage.Settings; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Flypaper.Storage; public static class FlypaperStorageExtensions { public static IHostApplicationBuilder AddFlypaperStorage(this IHostApplicationBuilder builder) { var connectionString = builder.Configuration.GetConnectionString("Flypaper") ?? "Data Source=flypaper.db"; var source = new DbConfigurationSource(connectionString); builder.Configuration.Add(source); builder.Services.AddSingleton(source); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.Configure(builder.Configuration.GetSection(RetentionOptions.SectionName)); builder.Services.AddFlypaperStorage(connectionString); return builder; } public static IServiceCollection AddFlypaperStorage(this IServiceCollection services, string connectionString) { services.AddDbContextFactory(options => options.UseSqlite(connectionString)); services.AddSingleton(); services.AddSingleton(sp => sp.GetRequiredService()); services.AddHostedService(); services.AddHostedService(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddHostedService(); return services; } public static async Task MigrateFlypaperDatabaseAsync(this IServiceProvider services, CancellationToken ct = default) { var factory = services.GetRequiredService>(); await using (var db = await factory.CreateDbContextAsync(ct)) { await db.Database.MigrateAsync(ct); } services.GetService()?.TryReload(); } }