using Anthropic.SDK; using IdentityModel; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection.Extensions; using Yavsc.Abstract.Interfaces; using Yavsc.Helpers; using Yavsc.Interface; using Yavsc.Interfaces; using Yavsc.Models; using Yavsc; using Yavsc.Server.Helpers; using Yavsc.Services; using Yavsc.Server.Hubs; internal class Program { private static async Task Main(string[] args) { Console.Title = "API"; var builder = WebApplication.CreateBuilder(args); builder.AddConfiguration("api"); var services = builder.Services; // Anthropic client builder.Services.AddSingleton(_ => new AnthropicClient( new APIAuthentication( builder.Configuration["ANTHROPIC_API_KEY"] ?? throw new InvalidOperationException("ANTHROPIC_API_KEY manquante") ) ) ); // Service de modération if (builder.Environment.IsDevelopment()) builder.Services.AddScoped(); else builder.Services.AddScoped(); // accepts any access token issued by identity server // adds an authorization policy for scope 'scope1' services .AddAuthorization(options => { options.AddPolicy("ApiScope", policy => { policy .RequireAuthenticatedUser() .RequireClaim(JwtClaimTypes.Scope, new string[] { "api" }); }); options.AddPolicy("AdministratorOnly", policy => { policy .RequireAuthenticatedUser() .RequireClaim(Constants.RoleClaimType, Constants.AdminGroupName); }); }) .AddYavscCors(builder.Configuration) .AddControllersWithViews(); // Binds the "Site" section (Bills, TempDir, Avatars, …) used by // the document-generation flow and BillingController. services.Configure(builder.Configuration.GetSection("Site")); // accepts any access token issued by identity server services.AddAuthentication("Bearer") .AddYavscJwtBearer(builder.Configuration); services.AddSignalR() // ChatRoomInfo et d'autres DTO de hub exposent des champs // publics (pas des propriétés) : STJ ne les sérialise pas // sans IncludeFields, et le client recevrait des objets vides. .AddJsonProtocol(options => options.PayloadSerializerOptions.IncludeFields = true); services.AddSingleton(); // DbContextBuilder services.AddDbContext(options => options.UseNpgsql(builder.Configuration.GetConnectionString( Yavsc.Constants.YavscConnectionStringName))); services.AddIdentityCore(options => { options.ClaimsIdentity.UserNameClaimType = JwtClaimTypes.PreferredUserName; options.ClaimsIdentity.RoleClaimType = Constants.RoleClaimType; }) .AddRoles() .AddEntityFrameworkStores() .AddDefaultTokenProviders() .AddSignInManager(); services.AddLocalization(options => { options.ResourcesPath = "Resources"; }); // services.AddTransient, MailSender>(); services.AddTransient() .AddTransient() .TryAddSingleton(); services .AddTransient() .AddTransient() .AddTransient(); services.AddTransient(); builder.Services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(30); options.Cookie.HttpOnly = true; options.Cookie.IsEssential = false; }); builder.Services.AddDistributedMemoryCache(); builder.Services.Configure(options => { var supportedCultures = new[] { "fr", "en", "pt" }; options.SetDefaultCulture(supportedCultures[0]) .AddSupportedCultures(supportedCultures) .AddSupportedUICultures(supportedCultures); }); WorkflowHelpers.ConfigureBillingService(); using (var app = builder.Build()) { if (app.Environment.IsDevelopment()) app.UseDeveloperExceptionPage(); app.UseMiddleware(); app .UseRouting() .UseAuthentication() .UseAuthorization() .UseCors("default"); app.MapIdentityApi().RequireAuthorization("ApiScope"); app.MapDefaultControllerRoute(); app.UseSession(); app.MapHub("/" + Constants.ChatHubPath); await app.RunAsync(); } } }