using System.Text; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; using Prometheus; using StackExchange.Redis; using VoidCat.Model; using VoidCat.Services; using VoidCat.Services.Abstractions; using VoidCat.Services.Migrations; var builder = WebApplication.CreateBuilder(args); var services = builder.Services; var configuration = builder.Configuration; var voidSettings = configuration.GetSection("Settings").Get(); services.AddSingleton(voidSettings); var seqSettings = configuration.GetSection("Seq"); builder.Logging.AddSeq(seqSettings); var useRedis = !string.IsNullOrEmpty(voidSettings.Redis); if (useRedis) { var cx = await ConnectionMultiplexer.ConnectAsync(voidSettings.Redis); services.AddSingleton(cx); services.AddSingleton(cx.GetDatabase()); } services.AddRouting(); services.AddControllers().AddNewtonsoftJson(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new() { ValidateIssuer = true, ValidateAudience = false, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = voidSettings.JwtSettings.Issuer, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(voidSettings.JwtSettings.Key)) }; }); // void.cat services services.AddVoidMigrations(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); if (useRedis) { services.AddScoped(); services.AddScoped(svc => svc.GetRequiredService()); services.AddScoped(svc => svc.GetRequiredService()); } else { services.AddMemoryCache(); services.AddScoped(); services.AddScoped(svc => svc.GetRequiredService()); services.AddScoped(svc => svc.GetRequiredService()); } var app = builder.Build(); // run migrations var migrations = app.Services.GetServices(); foreach (var migration in migrations) { await migration.Migrate(); } app.UseStaticFiles(); app.UseAuthentication(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(ep => { ep.MapControllers(); ep.MapMetrics(); ep.MapFallbackToFile("index.html"); }); app.Run();