yavsc/src/Yavsc.Api/Program.cs

141 lines
5 KiB
C#
Raw Normal View History

2025-02-11 04:45:05 +00:00
/*
Copyright (c) 2024 HigginsSoft, Alexander Higgins - https://github.com/alexhiggins732/
Copyright (c) 2018, Brock Allen & Dominick Baier. All rights reserved.
Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
Source code and license this software can be found
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
2026-06-06 21:10:48 +01:00
using Anthropic.SDK;
2025-02-15 19:57:08 +00:00
using IdentityModel;
2025-02-11 04:45:05 +00:00
using Microsoft.AspNetCore.Mvc;
2025-02-14 00:20:35 +00:00
using Microsoft.EntityFrameworkCore;
2025-08-18 11:27:13 +01:00
using Yavsc;
2026-06-06 21:10:48 +01:00
using Yavsc.Abstract.Interfaces;
2025-02-26 18:59:08 +00:00
using Yavsc.Helpers;
2025-02-15 12:45:27 +00:00
using Yavsc.Interface;
2025-02-14 00:20:35 +00:00
using Yavsc.Models;
2025-02-15 12:45:27 +00:00
using Yavsc.Services;
2025-02-11 04:45:05 +00:00
internal class Program
{
private static async Task Main(string[] args)
{
Console.Title = "API";
var builder = WebApplication.CreateBuilder(args);
var services = builder.Services;
2026-06-06 21:10:48 +01:00
// 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>();
2025-02-11 04:45:05 +00:00
// accepts any access token issued by identity server
2025-02-15 19:57:08 +00:00
// adds an authorization policy for scope 'scope1'
2025-02-17 23:56:28 +00:00
2025-02-11 04:45:05 +00:00
services
.AddAuthorization(options =>
{
options.AddPolicy("ApiScope", policy =>
{
policy
.RequireAuthenticatedUser()
2025-02-17 23:56:28 +00:00
.RequireClaim(JwtClaimTypes.Scope, new string[] { "scope2" });
2025-02-11 04:45:05 +00:00
});
})
.AddCors(options =>
{
// this defines a CORS policy called "default"
options.AddPolicy("default", policy =>
{
policy.WithOrigins("https://localhost:5003")
.AllowAnyHeader()
.AllowAnyMethod();
});
})
2025-02-15 19:57:08 +00:00
.AddControllers();
2025-02-11 04:45:05 +00:00
// accepts any access token issued by identity server
2025-02-15 19:57:08 +00:00
var authenticationBuilder = services.AddAuthentication("Bearer")
2025-02-11 04:45:05 +00:00
.AddJwtBearer("Bearer", options =>
{
options.IncludeErrorDetails = true;
options.Authority = "https://localhost:5001";
options.TokenValidationParameters =
2026-05-30 19:34:22 +01:00
new() { ValidateAudience = false, RoleClaimType = YavscConstants.RoleClaimType };
2025-07-07 07:49:18 +01:00
options.MapInboundClaims = true;
2025-02-17 23:56:28 +00:00
});
2026-06-04 22:04:20 +01:00
2025-02-17 23:56:28 +00:00
services.AddDbContext<ApplicationDbContext>(options =>
2026-06-04 22:04:20 +01:00
2025-02-17 23:56:28 +00:00
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
2026-06-04 22:04:20 +01:00
services.AddLocalization(options =>
{
options.ResourcesPath = "Resources";
});
//
services.AddTransient<Microsoft.AspNetCore.Identity.IEmailSender<ApplicationUser>, MailSender>();
2025-02-17 23:56:28 +00:00
services.AddTransient<ITrueEmailSender, MailSender>()
2026-06-04 22:04:20 +01:00
.AddTransient<Microsoft.AspNetCore.Identity.UI.Services.IEmailSender,
MailSender>()
2025-02-17 23:56:28 +00:00
.AddTransient<IBillingService, BillingService>()
.AddTransient<ICalendarManager, CalendarManager>();
2025-02-26 18:59:08 +00:00
services.AddTransient<IFileSystemAuthManager, FileSystemAuthManager>();
2026-06-04 22:04:20 +01:00
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);
});
2025-02-26 18:59:08 +00:00
WorkflowHelpers.ConfigureBillingService();
2025-02-11 04:45:05 +00:00
using (var app = builder.Build())
{
if (app.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();
app
.UseRouting()
.UseAuthentication()
2025-02-16 17:28:38 +00:00
.UseAuthorization()
.UseCors("default")
2025-02-16 22:40:51 +00:00
;
2026-06-04 22:04:20 +01:00
app.MapIdentityApi<ApplicationUser>().RequireAuthorization("ApiScope");
2025-02-16 22:40:51 +00:00
app.MapDefaultControllerRoute();
2025-02-16 17:28:38 +00:00
app.MapGet("/identity", (HttpContext context) =>
new JsonResult(context?.User?.Claims.Select(c => new { c.Type, c.Value }))
2025-07-07 07:49:18 +01:00
);
2025-02-16 17:28:38 +00:00
2026-06-04 22:04:20 +01:00
app.UseSession();
2025-02-11 04:45:05 +00:00
await app.RunAsync();
2025-02-17 23:56:28 +00:00
}
2025-02-11 04:45:05 +00:00
}
}