using System.Security.Claims; using MaxMind.GeoIP2; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization.Infrastructure; using Microsoft.EntityFrameworkCore; using Nostr.Client.Client; using NostrStreamer.Database; using NostrStreamer.Services; using NostrStreamer.Services.Background; using NostrStreamer.Services.Dvr; using NostrStreamer.Services.StreamManager; using NostrStreamer.Services.Thumbnail; namespace NostrStreamer; internal static class Program { public static async Task Main(string[] args) { var builder = WebApplication.CreateBuilder(args); var services = builder.Services; var config = builder.Configuration.GetSection("Config").Get(); ConfigureDb(services, builder.Configuration); services.AddCors(); services.AddMemoryCache(); services.AddHttpClient(); services.AddRazorPages(); services.AddControllers().AddNewtonsoftJson(); services.AddSingleton(config); // GeoIP services.AddSingleton(_ => new DatabaseReader(config.GeoIpDatabase)); services.AddTransient(); // nostr auth services.AddTransient(); services.AddAuthentication(o => { o.DefaultChallengeScheme = NostrAuth.Scheme; o.AddScheme(NostrAuth.Scheme, "Nostr"); }); services.AddAuthorization(o => { o.DefaultPolicy = new AuthorizationPolicy(new[] { new ClaimsAuthorizationRequirement(ClaimTypes.Name, null) }, new[] {NostrAuth.Scheme}); }); // nostr services services.AddSingleton(); services.AddSingleton(s => s.GetRequiredService()); services.AddSingleton(); services.AddHostedService(); services.AddTransient(); // streaming services services.AddTransient(); services.AddHostedService(); services.AddSingleton(); services.AddHostedService(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddHostedService(); services.AddTransient(); // lnd services services.AddSingleton(); services.AddHostedService(); var app = builder.Build(); using (var scope = app.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); await db.Database.MigrateAsync(); } app.UseCors(o => o.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin()); app.UseAuthorization(); app.MapRazorPages(); app.MapControllers(); await app.RunAsync(); } private static void ConfigureDb(IServiceCollection services, IConfiguration configuration) { services.AddDbContext(o => o.UseNpgsql(configuration.GetConnectionString("Database"))); } /// /// Dummy method for EF core migrations /// /// /// // ReSharper disable once UnusedMember.Global public static IHostBuilder CreateHostBuilder(string[] args) { var dummyHost = Host.CreateDefaultBuilder(args); dummyHost.ConfigureServices((ctx, svc) => { ConfigureDb(svc, ctx.Configuration); }); return dummyHost; } }