Some checks failed
Dotnet build and test / build (pull_request) Has been cancelled
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
150 lines
5.5 KiB
C#
150 lines
5.5 KiB
C#
|
|
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<AnthropicClient>(_ =>
|
|
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<IModerationService, MockModerationService>();
|
|
else
|
|
builder.Services.AddScoped<IModerationService, ClaudeModerationService>();
|
|
|
|
|
|
// 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<SiteSettings>(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<IConnexionManager, HubConnectionManager>();
|
|
|
|
// DbContextBuilder
|
|
services.AddDbContext<ApplicationDbContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString(
|
|
Yavsc.Constants.YavscConnectionStringName)));
|
|
|
|
services.AddIdentityCore<ApplicationUser>(options =>
|
|
{
|
|
options.ClaimsIdentity.UserNameClaimType = JwtClaimTypes.PreferredUserName;
|
|
options.ClaimsIdentity.RoleClaimType = Constants.RoleClaimType;
|
|
})
|
|
.AddRoles<IdentityRole>()
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
.AddDefaultTokenProviders()
|
|
.AddSignInManager();
|
|
|
|
services.AddLocalization(options =>
|
|
{
|
|
options.ResourcesPath = "Resources";
|
|
});
|
|
//
|
|
services.AddTransient<Microsoft.AspNetCore.Identity.IEmailSender<ApplicationUser>, MailSender>();
|
|
services.AddTransient<ITrueEmailSender, MailSender>()
|
|
.AddTransient<Microsoft.AspNetCore.Identity.UI.Services.IEmailSender,
|
|
MailSender>()
|
|
.TryAddSingleton<ISmtpClientFactory, SmtpClientFactory>();
|
|
services
|
|
.AddTransient<IBillingService, BillingService>()
|
|
.AddTransient<ICalendarManager, CalendarManager>()
|
|
.AddTransient<IYavscMessageSender, YavscMessageSender>();
|
|
services.AddTransient<IFileSystemAuthManager, FileSystemAuthManager>();
|
|
builder.Services.AddSession(options =>
|
|
{
|
|
options.IdleTimeout = TimeSpan.FromMinutes(30);
|
|
options.Cookie.HttpOnly = true;
|
|
options.Cookie.IsEssential = false;
|
|
});
|
|
|
|
builder.Services.AddDistributedMemoryCache();
|
|
|
|
builder.Services.Configure<RequestLocalizationOptions>(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<ResourceUsageTelemetryMiddleware>();
|
|
app
|
|
.UseRouting()
|
|
.UseAuthentication()
|
|
.UseAuthorization()
|
|
.UseCors("default");
|
|
app.MapIdentityApi<ApplicationUser>().RequireAuthorization("ApiScope");
|
|
app.MapDefaultControllerRoute();
|
|
app.UseSession();
|
|
app.MapHub<ChatHub>("/" + Constants.ChatHubPath);
|
|
await app.RunAsync();
|
|
}
|
|
}
|
|
}
|